Thread: FileName

  1. #1
    Registered User
    Join Date
    Jan 2005


    Using MS Visual Studio C++

    I am using a function that I wrote which will create a string. The string will have the date & time which is preceeded by a letter that is a function arg to identify the output file type. This string is used to make the output file for the program. However, when I use this function to output two different files, the underscore is used in the first file, but on the second use, the underscore turns into a 0 (zero). This happens when I change the underscore to a random ascii character. Please view the code. I'd like to know how I can use the underscore in both filenames that are created consecutively.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Your attached files are missing information and are generally illegible.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    I'm guessing that he is having some trouble with this function.
    char *OutNameTime(char *cLetter)	
    	char *cTimeFile;
    	struct tm *newtime;
        time_t long_time;
        time(&long_time);						// Get time as long integer. 
        newtime = localtime(&long_time);		// Convert to local time. 
    	char *chTime = asctime(newtime);
    	char cFileStr[17];
    	//	This is for the year
    	cFileStr[0] = chTime[20];
    	cFileStr[1] = chTime[21];
    	cFileStr[2] = chTime[22];
    	cFileStr[3] = chTime[23];
    	//	This is for the month
    	cFileStr[4] = chTime[4];
    	cFileStr[5] = chTime[5];
    	cFileStr[6] = chTime[6];
    	//	This is for the day
    	cFileStr[7] = chTime[8];
    	cFileStr[8] = chTime[9];
    	char *cUnderScore = "_";
    	cFileStr[9] = *cUnderScore;
    	//	This is for the time
    	cFileStr[10] = chTime[11];
    	cFileStr[11] = chTime[12];
    	cFileStr[12] = chTime[14];
    	cFileStr[13] = chTime[15];
    	cFileStr[14] = chTime[17];
    	cFileStr[15] = chTime[18];
    	char *cPath = "C:\\TrackerFiles/Tracker2File/";
    	cTimeFile = cPath;
    	cTimeFile[29] = *cLetter;
    	cTimeFile[30] = cFileStr[0];
    	cTimeFile[31] = cFileStr[1];
    	cTimeFile[32] = cFileStr[2];
    	cTimeFile[33] = cFileStr[3];
    	cTimeFile[34] = cFileStr[4];
    	cTimeFile[35] = cFileStr[5];
    	cTimeFile[36] = cFileStr[6];
    	cTimeFile[37] = cFileStr[7];
    	cTimeFile[38] = cFileStr[8];
    	cTimeFile[39] =	cFileStr[9];
    	cTimeFile[40] = cFileStr[10];
    	cTimeFile[41] = cFileStr[11];
    	cTimeFile[42] = cFileStr[12];
    	cTimeFile[43] = cFileStr[13];
    	cTimeFile[44] = cFileStr[14];
    	cTimeFile[45] = cFileStr[15];
    	cTimeFile[46] = NULL;

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    I can't read those files, not all of them at least, and they are not wrapped/formatted. Anyway, what happens if you...

    cFileStr[9] = '_';

    ... instead?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Wouldn't strftime make this much simpler?
    char *OutNameTime(char *cLetter)
       static char cTimeFile [ 47 ];
       time_t now = time(0);
       struct tm *local = localtime(&now);
       if ( local )
          strftime(cTimeFile, sizeof cTimeFile,
                   "C:\\TrackerFiles/Tracker2File/?%Y%b%d_%H%M%S", local);
          cTimeFile[29] = *cLetter;
       return cTimeFile;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Time as filename
    By rkooij in forum C Programming
    Replies: 8
    Last Post: 03-02-2006, 09:17 AM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM