|
-
Aug 2nd, 2001, 07:11 AM
#1
Thread Starter
Lively Member
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
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Aug 2nd, 2001, 11:35 AM
#2
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. 
Destined
-
Aug 2nd, 2001, 01:34 PM
#3
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;
-
Aug 3rd, 2001, 04:30 AM
#4
Thread Starter
Lively Member
Cheers Guys
Thats exactly what i need thanks for the help.
Peter
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|