-
Filenames
Hey Guys I need help again suprise suprise
I must say i do appreciate all the help that people give me and hopefully i will be in the position to help other people soon.
Anyway i was wondering if anyone knew a simple way to take a string which contains a filename in this case and chop the last three characters of basically the file extension and then reapply another extension.
the string for the filename is a TCHAR type
Thanks for the help
Peter
-
Heh.. I'm not sure how 'valid' this is for editing strings, but it works for me:
PHP Code:
TCHAR FileName[128] = "Hello world.jpg";
cout << FileName << endl;
int i = 0;
while (FileName[i] != '\\0') i++;
FileName[i-3] = '\\0';
strcat(FileName,"gif"); // need <string.h> for this
cout << FileName << endl;
I stated that this might not be "valid" as I don't replace the 'p' or 'g' with string termination characters ('\0'), but it does work. Another method, after the while loop, is to use
PHP Code:
i -= 3;
FileName[i++] = 'g';
FileName[i++] = 'i';
FileName[i++] = 'f';
cout << FileName << endl;
If you're not going for smallest filesize, the top one is a bit neater, plus you could replace "gif" with a string passed to that function.
Hope this is what you need. :D
Destined
-
Try this:
Code:
char sNewName[25];
char *sFile = "My File.jpg";
for(int i=0; i < strlen(sFile); i++) {
if( sFile[i] == '.' ) {
strncpy(sNewName, sFile, i);
sNewName[i] = '\0';
}
}
// sNewName contains the filename without the extension.
cout << sNewName;
-
Cheers Guys
Thats exactly what i need thanks for the help.
Peter