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):
| Name |
strchr, strrchr - locate character in string
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.
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( ptr, suffix); //suffix, same as before
printf("%s\n", file_out); //it worked?
As I said, file_out has to be large enough.