I am using Linux GCC c99.
I am wondering what would be the best technique. To change a string. I am using strstr();
I have a filename called ‘file.vce’ and I want to change the extension to ‘file.wav’.
Is this the best method:
char file_name[80] = 'filename.vce'; char *new_file_name = NULL; new_file_name = strstr(file_name, 'vce'); strncpy(new_file_name, 'wav', 3); printf('new file name: %s\n', new_file_name); printf('file name: %s\n', file_name);
Many thanks for any advice,
I have edited my answer using using your suggestions. Can you see anything else wrong?
/** Replace the file extension .vce with .wav */ void replace_file_extension(char *file_name) { char *p_extension; /** Find the extension .vce */ p_extension = strrchr(file_name, '.'); if(p_extension) { strcpy(++p_extension, 'wav'); } else { /** Filename did not have the .vce extension */ /** Display debug information */ } } int main(void) { char filename[80] = 'filename.vce'; replace_file_extension(filename); printf('filename: %s\n', filename); return 0; }
I would do this
You NEED to do pointer manipulation in every c program. Of course you’d do some more checking for buffer over runs etc. or even use the path specific functions.