Results 1 to 4 of 4

Thread: Filenames

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94

    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]

  2. #2
    Destined Soul
    Guest
    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

  3. #3
    Megatron
    Guest
    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;

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    England
    Posts
    94
    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
  •  



Click Here to Expand Forum to Full Width