CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    2

    TimeStamp and printing a string to a text file

    Hi,

    I've just started using C++ and I keep getting stuck and could use some help. Currently I am using a SDK that controls a camera I got from a company (I'm doing a project for school). The SDK included an app that was built with MFC. I'd like to save an image in "bmp" format with a time stamp but I am not having a much luck. Here is what my code currently looks like:

    char mystring[200] = "C:\\";
    time_t mytime = time(0);
    strcpy(tempStuff, ctime(&mytime));

    char test[] = ".bmp";
    strcat(mystring,tempStuff);
    strcat(mystring,test);

    The issue is that when I try to save it with the time stamp my picture doesn't show up anywhere. If I remove the timestamp and just concat mystring to test (skipping tempStuff), I would be able to get the image.

    Another thing I'd like to do is save the filename string into a text file but I have no idea how to do this. Can someone help me?

    Thank you.

  2. #2
    Join Date
    Mar 2010
    Posts
    2

    Re: TimeStamp and printing a string to a text file

    Ok so I realized my issue when I was able figure out how to print a string to a text file through C++ code. The issue is that when I store the timestamp into the string it adds a new line to the ending causing a problem when trying to include .bmp extension. Anyone have an idea how I can get rid of the new line command that is inherently within the timestamp code?

    Thank you.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: TimeStamp and printing a string to a text file

    ctime appends a CR/LF to it's output. You can truncate it with say, strtok:

    [code]
    time_t mytime = time(0);
    strcpy(tempStuff, ctime(&mytime));
    strtok(tempStuff,"\r\n");
    [code]

    but using MFC, you might be better off using a CString and CTime and associated methods to get the desired output.

  4. #4
    Join Date
    Nov 2001
    Posts
    251

    Re: TimeStamp and printing a string to a text file

    Quote Originally Posted by hoxsiew View Post
    ctime appends a CR/LF to it's output. You can truncate it with say, strtok:

    [code]
    time_t mytime = time(0);
    strcpy(tempStuff, ctime(&mytime));
    strtok(tempStuff,"\r\n");
    [code]

    but using MFC, you might be better off using a CString and CTime and associated methods to get the desired output.
    Don't use strtok. It's not thread-safe. Just zero out the last character in the string.
    Last edited by Syslock; March 8th, 2010 at 11:03 AM.

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: TimeStamp and printing a string to a text file

    Neither is ctime(), so I assumed that wasn't a concern of the OP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured