Truncating a char *

This is a discussion on Truncating a char * within the C++ Programming forums, part of the General Programming Boards category; Hey all, Probably this is a easy one but not sure how to do it. This is what I have ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    45

    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.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you can use strchr() to locate the '.' and then overwrite that location with '\0'

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    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;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Thanks to both of you each method clued me in on what to think about and I appreciate your efforts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21