1. The bad and the ugly...
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Oz... No??? Neverland then?
    Rep Power

    Can someone explain this code snippet?

    char *suffix = "_out";
    char *file_out;
    char *ptr;
    //create filename_out.txt
    file_out = (char *)malloc((x) * sizeof(char));
    strcpy( file_out, fname); //prefix
    ptr = strchr(file_out, '.'); //what does this do? found command online...
    strcpy( ptr, suffix); //suffix
    printf("%s\n", file_out); //it worked?
    so user inputs 'filename.txt' and my program will eventually sort it, then it creates 'filename_out' and writes the sorted stuff to the new '_out' file.

    can someone explain that strchr and the use of the *ptr?

    also, if i was to then open the new filename_out, would i also need to append '.txt' to it?
    char *suffix = "_out";
    //should actually be:
    char *suffix = "_out.txt";
    "Life is not a journey with the intent on arriving at the finish line in a pretty and well preserved body. But rather to skid in broadside, totally worn out, thoroughly used up and loudly proclaiming, "Wow! What a ride!" -Anonymous
    Halo! || Diablo 2 LOD Modding || OLGA's BACK!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    From early in programming lore there comes an acronym: "RTFM" ("Read The F***ing Manual!", though the "F" is usually euphemized to "furnished" or "fine" or something else). It would be yelled at programmers who asked others to tell him what was easily looked up in the manual -- that's analogous to people who post here asking us to do Google searches for them, which does happen believe it or not.

    Of course, I'm telling you this in the nicest way possible. If you don't know what a standard library function does or what its return values mean, then your first step would be to look it up yourself. Even if your compiler did not come with its own documentation, the man pages (the older UNIX/Linux form of documentation for C and for shell scripts) are readily available on-line. One very good reason for looking up the man page first is that you get much faster response than by consulting a forum.

    For example, in Google'ing on man page strchr, the first of the 40,800 hits that produced was http://linux.die.net/man/3/strchr, which says (editing out one function):

    strchr, strrchr - locate character in string


    #include <string.h>
    char *strchr(const char *s, int c);
    char *strrchr(const char *s, int c);


    The strchr() function returns a pointer to the first occurrence of the character c in the string s.

    The strrchr() function returns a pointer to the last occurrence of the character c in the string s.

    Here "character" means "byte"; these functions do not work with wide or multibyte characters.

    Return Value

    The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found.
    So then, after you have copied the input string containing the filename into another string, this line,
    ptr = strchr(file_out, '.');
    , searches that string for the first instance of a period, which presumably separates the filename and the file extension. ptr is a char pointer that points to that first period. Then this following line,
    strcpy( ptr, suffix);
    , copies the suffix string starting at ptr, which overwrites the period and the old file extension. As you should be able to observe thanks to that printf statement.

    As you observed, if you wanted filename_out to have a .txt file extension, then you would need to add it, either by changing suffix or by concatenating it onto the new filename, eg with strcat(). In any case you would need to ensure that the value of x would make file_out large enough to hold all those characters plus the null-terminator; it doesn't matter if it's a bit too large, but it does matter if it's too small.

    Another thought could be to save whatever file extension the input filename has and to then append it onto the new filename. After the strchr (which should be tested for NULL to ensure that it did indeed find a period), you could then do:
        ptr = strchr(file_out, '.'); // same as before
        ext = (char*)malloc((strlen(ptr)+1)*sizeof(char)); // char *ext;
        strcpy(ext, ptr);
        strcpy( ptr, suffix); //suffix, same as before
        strcat(file_out, ext);
        printf("%s\n", file_out); //it worked?
    As I said, file_out has to be large enough.

IMN logo majestic logo threadwatch logo seochat tools logo