-
2 Attachment(s)
FileName
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.
-
Your attached files are missing information and are generally illegible.
-
I'm guessing that he is having some trouble with this function. ;)
Code:
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;
return(cTimeFile);
}
-
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?
-
Wouldn't strftime make this much simpler? Code:
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;
}