Thread: how do I take a filename and copy it into a new file with a different extension?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    how do I take a filename and copy it into a new file with a different extension?

    Like I want to make a copy of yar.txt how do I use that filename and add a .cpy to it?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read this thread on the same topic.

    How do I make a program with two arguments in dos

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Assuming you have two strings, one with the filename, and the other with the new extension, you could always copy both strings into a third string with strncpy().

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Method one...
    Code:
    char NewName[255] = {0};
    
    strcpy(NewName,OldName);
    strcat(NewName,".cpy");
    
    // result:  filename.txt.cpy
    Method 2...
    Code:
    char NewName[255] = {0};
    char* ptr;
    
    strcpy(NewName,OldName);
    ptr = strchr(NewName,'.');
    strcpy(ptr,".cpy");
    
    // result... filename.txt becomes filename.cpy

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    thanks

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I am trying it this way:
    Code:
    char NewName[250] = {0};
        char Oldname[250]={argv[1]};
        strcpy(NewName,Oldname);
        strcat(NewName,".cpy");
    but it isn't working, it compiles ok but when run with the filename after the exe as test.txt it just doesn't work

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That should generate warnings/errors for your compiler. If not, crank up the warning settings.

    Then, you need to strcpy argv[1] into Oldname. Also, you should pick a consistent formatting for variable names. Either capitalize the 'N' in "Name" for NewName and OldName or don't, but be consistent.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Can you be a little more specific.
    Do you want to copy the filename or the contents of the file?

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    copy both but make the copy of the file have an extra extension on the end like .cpy

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You don't need to save argv[1], you can use that throughout your program. Here's another thing you can try, without length limits on the filename. You will need to add file operations and use new_name for the file you write to.

    Code:
    char *new_name = malloc(strlen(argv[1]) + 1 + 4); /* +1 for \0, +4 for .cpy */
    strcpy(new_name, argv[1]);
    strcat(new_name, ".cpy");
    
    puts(new_name);
    
    free(new_name);

  11. #11
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Some psuedocode:

    Code:
    int main(int argc, char *argv[])
    
    if argv[1] is null, printf usage message
    
    argv[1] will be file to be copied
    
    FILE *in = fopen argv[1], mode "r"
    
    FILE *out = fopen argv[1] with ".cpy" added on, mode "w"
    
    int x;
    while ((x = fgetc(in)) != EOF) {
        fputc(x, out)
    }
    printf("SUCCESS!!!\n");
    Remember: they're called file streams for a reason. When you read a byte from an input stream (or write a byte to an output stream), it will automatically advance you to the next character, so you don't overwrite the last character you wrote, and you don't read the same byte again. This is much more efficient than copying the entire input file into memory, and writing the entire output file from memory.

    Refer to the other posts in this thread for working with strings.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Once-ler View Post
    I am trying it this way:
    Code:
    char NewName[250] = {0};
        char Oldname[250]={argv[1]};
        strcpy(NewName,Oldname);
        strcat(NewName,".cpy");
    but it isn't working, it compiles ok but when run with the filename after the exe as test.txt it just doesn't work
    This is what happens when you do scoop and poop coding... I gave you an EXAMPLE... not a finished product.

    Code:
    int main ( int argc, char* argv[] )
      {
         char OutputFile[256] = {0};
    
         // part of your program code here
    
        strcpy(OutputFile, argv[1]);
        strcat(outputFile,".cpy");
        fp = fopen(OutputFile,"w");
        if (! fp)
          { 
             printf("Could not open %s",OutputFile);
             exit(-1);
           }
    
        // rest of program here
    
       return 0;
    }
    Get the idea... I gave you a general suggestion... you need to expand and modify it to your needs.
    Last edited by CommonTater; 01-10-2012 at 09:39 PM.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing filename extension
    By slippy in forum C Programming
    Replies: 3
    Last Post: 05-03-2007, 11:04 AM
  2. Replies: 6
    Last Post: 04-11-2006, 04:52 PM
  3. .ef file extension
    By xddxogm3 in forum Tech Board
    Replies: 8
    Last Post: 02-08-2006, 06:20 AM
  4. allow another file extension??
    By C+noob in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-03-2005, 08:52 PM
  5. get file extension
    By Nada in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 09:12 AM