-
Truncating a char *
Hey all,
Probably this is a easy one but not sure how to do it. This is what I have
Code:
int month=0;
char *filename2[] = {
"january.log", "february.log",
"march.log", "april.log", ......};
while (month<12)
{
fout<<filename2[month]<<" Sales"<<endl;
}
Is there a way to truncate the ".log" easily? This is the jist of my file some code errors are probably here but again its just the jist.
-
you can use strchr() to locate the '.' and then overwrite that location with '\0'
-
It might be easiest to simply use the stream's write member function to output a set number of characters:
Code:
while( month < 12 )
{
// Use write to output up to (not including) the '.' character
fout.write(filename2[month],strchr(filename2[month],'.')-filename2[month]);
fout << " Sales" << endl;
++month;
}
This way the original data does not get modified by overwritting characters.
Just as easy to use the string class to print out some temporaries on the way through the loop:
Code:
int month = 0;
string filename2[] = { "january.log", ... };
while( month < 12 )
{
fout << string(filename2[month],0,filename2[month].find(".log")) << " Sales" << endl;
++month;
}
-
Thanks to both of you each method clued me in on what to think about and I appreciate your efforts.