Thread: Changing filename extension

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Changing filename extension

    Hi,

    My program takes two (filename) arguments, and I want the second argument to change to filename2C.dat

    [slippy@slipnslidn ~]% ./program filename1.dat filename2.dat

    Code:
     
    FILE* fn, fm, fo;
    
    int 
    main(int* argc, char** argv){
    
    fn = fopen(argv[1], "r");
    fm = fopen(argv[2], "r");
    
    /* This doesn't work... */
    
    fo = fopen(strcat(strstr(argv[2], "."), "C.dat", "w"));
    
    /* Do something */
    
    /* Shut things down */
    
    close(fn);
    close(fm);
    close(fo);
    }
    Thanks for your help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You should copy the command-line argument to a temp buffer and work on that, not on the argument directly. Aside from that, the strcat should probably be a strcpy instead and one of the right parenthesis is in the wrong place... it should be after the "C.Dat" string and not after the "w".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > strcat(strstr(argv[2], "."), "C.dat", "w")
    Split this out into it's component steps.
    For starters, strstr() can return NULL, which leaves you up the creek without a paddle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    That won't work because it uses memory improperly. You need to allocate enough space for the string and built it yourself:

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    char *dot, *newname;
    int len;
    
    /* Get position of the last dot in the name */
    dot = strrchr(name, '.');
    /* If a dot was found, calculate the length to this point */
    if(dot) len = dot - name;
    else len = strlen(name);
    /* Allocate a buffer big enough to hold the result.
     * 6 extra characters: 5 for "C.dat", 1 for null byte */
    newname = malloc(len + 6);
    /* Build the new name */
    strncpy(newname, name, len);
    strcpy(newname + len, "C.dat");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. Changing filename to all capitals
    By cosmiccomputing in forum C Programming
    Replies: 9
    Last Post: 06-16-2008, 02:42 AM
  4. Replies: 6
    Last Post: 04-11-2006, 04:52 PM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM