Thread: Help to change a file extension

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Help to change a file extension

    This is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int parse (char **buf, const char *src, const char *ext)
    {
    	const char *cpy = src; /* make a copy */
    	int len = 0; /* total size of buf */
    	int pos = 0; /* grab the last dot found */
    	int index = 0; /* current iterator character */
    	int stop = 1; /* stop the loop */
    	while (*cpy)
    	{
    		/* find the last dot in the string */
    		if (*cpy == '.')
    		{
    			pos = index; /* save the position */
    		}
    		cpy++; /* next char */
    		index++; /* len of the string */
    	}
    	len = pos; /* save the position of the last dot in the string */
    	index = 0;/* reset the counter */
    	for (;*src || !stop;pos++,src++)
    	{
    		/* now locate the position of the dot to ... */
    		if (pos == index)
    		{
    			/* copy the new file extension */
    			while (*ext)
    			{
    				**buf = *ext;
    				ext++;
    				*buf++;
    				len++;
    			}
    			**buf++ = '\0'; /* add null terminated char */
    			stop = 0; /* since we done, stop the loop */
    		}
    		**buf = *src; /* copying ... */
    		*buf++;
    	}
    	return len; /* return len of bufy */
    }
    
    int main (int argc, char *argv[])
    {
    	char *bufy = (char *) malloc (256);
    	int ret = parse (&bufy, "/home/joel/foo.flv", "mp3");
    	printf ("New path: %s\n", bufy);
    	free (bufy);
    	return 0;
    }
    "bufy", should return something like: /home/joel/foo.mp3, but instead it crashes
    Any ideas?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Holy crap... I generally just use...
    Code:
    char *x;
    
    x = strrchr(string,'.');
    strcpy(x,extension);
    Last edited by CommonTater; 04-28-2011 at 09:57 AM.

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Yes, I know...but I'm trying to do it for myself
    plus, learn about to use "&buffy"
    Last edited by Joelito; 04-28-2011 at 10:21 AM.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Joelito View Post
    Yes, I know...but I'm trying to do it for myself
    plus, learn about to use "&buffy"
    Then I suggest you learn to think in smaller blobs.... Write a simple function to find the dot and return a pointer. Write a second function to copy the extension to the pointer from the first function. Functions are pest kept to single purposes... one calculation, one operation, etc. Where you get into trouble is when you try to write one function that does 3245 different things all at once...

    For example, your find last dot function could look something like this...
    Code:
    char * FindLastDot(char* str)
      { char *ptr = str;
         char *dot = NULL;
    
         while(*ptr++)
           { if (*ptr == '.')
                dot = ptr; }
     
        return dot; }
    Smaller blobs make for easier coding and much easier debugging.
    Last edited by CommonTater; 04-28-2011 at 10:30 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Congratulations Joelito for trying to do this by yourself. But there were a lot of problems with your code.
    - the parse() function should have char *buf as its first parameter. Not **
    - any assigns should be *buf = , not **buf =
    - There is no need to have a nested while loop inside the for loop.
    - if (pos == index) should be if (index > len)
    - you need to put the *buf = *src in an else block
    - you need to increment index in the loop
    - do not put null terminator inside the loop. Put *buf = '\0' outside at the end

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC :: Multiple File Extension Support
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 06-02-2003, 09:18 AM
  2. Audio CD File Extension
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-17-2003, 10:47 PM
  3. finding the extension of a file
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2002, 06:36 PM
  4. get file extension
    By Nada in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 09:12 AM
  5. How to see if a file exists with a different extension
    By PCJason in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2001, 07:33 PM