Like Tree1Likes
  • 1 Post By laserlight

Managing strings, renaming problems

This is a discussion on Managing strings, renaming problems within the C Programming forums, part of the General Programming Boards category; Hi, I'd like to make a program which does this renaming work: example.txt -> example(copy).txt Also, I'd like it to ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    13

    Managing strings, renaming problems

    Hi, I'd like to make a program which does this renaming work:

    example.txt -> example(copy).txt

    Also, I'd like it to support any number of '.' into the file name, like:

    example.2011.txt -> example.2011(copy).txt

    I've tried using string.h functions... and I didn't think it could be that hard.

    I don't need help about the file managing (fopen, rename, etc). I just need help finding the correct position where to add the "(copy)".

    I've been working on it for some hours... then I decided to ask here because probably the code became too complex for the simple work.

    Here it is:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	char **ppc; // to visit **argv
    	char *est; // where to attach strings' extension
    	char *result; // resultant filename
    	int dim; // string length
    	int dim2; // 2nd string length
    	int i; // this serves like an index
    	int i2; // used to find filename's first part, then to copy the extension part
    	int i3;
    
        if(argc > 1)
        {
        	ppc = argv + 1;
        	for(i = 1; i < argc; ++i)
        	{
        		printf("%s renamed to ", *ppc);
    			dim = strlen(*ppc);
    			est = strrchr(*ppc, '.');
    			i2 = est - *ppc;
    			printf("\ni2 = %d (number of letters before the last '.')\n", i2);
    			result = strncpy(result, *ppc, i2);
    			printf("\nresult: %s\n", result);
    			i3 = i2 = dim - i2;
    			printf("\ni2: %d\n", i2);
    			while(i2-- > 0)
    			{
    				printf("%c", *est++);
    				//est++;
    				//*est = *est;
    				//*(++est) = *(est+1);
    			}
    			//est = est - i2
    			*(est) = '\0';
    			est = est - i3;
    			printf("\nextension: %s\n", est);
    			dim2 = strlen(est);
    			//result = strncpy(result, *ppc, dim - dim2);
    			result = strcat(result, "(copy)");
    			result = strcat(result, est);
    			printf("%s\n", result);
        	}
    	}
    
        return 0;
    }
    If you are on *nix, you can use the terminal and try "./binaryname example.txt" and it will work. But when I tried "./binaryname example.2011.txt" it printed some weird characterhs through the name.

    Where do I make errors?
    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    	char *result; // resultant filename
    If you are going to use result as though it pointed somewhere useful (such as, say, "result = strncpy(result, *ppc, i2);"), then you should make sure that it does, in fact, point somewhere useful.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Code:
    	char *result; // resultant filename
    If you are going to use result as though it pointed somewhere useful (such as, say, "result = strncpy(result, *ppc, i2);"), then you should make sure that it does, in fact, point somewhere useful.
    Ehm... what's wrong with that "result = strncpy(result, *ppc, i2);" ? In that part i want it to point to the "first part" of the filename, the part which is before the (copy) string (which I add it on the last part).
    If you wanted to tell me something else, please write it again in another form maybe I don't get it due to english problems.

    Thanks!

    edit: maybe I got it. When I use strncpy, does it allocate the space and assign it to result... or not?? Is this the problem?
    Last edited by kr0n1x; 09-02-2011 at 10:50 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kr0n1x View Post
    edit: maybe I got it. When I use strncpy, does it allocate the space and assign it to result... or not?? Is this the problem?
    strncpy does no space allocating at all.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    13
    Quote Originally Posted by tabstop View Post
    strncpy does no space allocating at all.
    Yay I got it

    #include malloc.h

    then, right before the strncpy instruction, I put:
    result = malloc(i2);

    Now it seems to work thanks! I will complete it for multiple files.

    Rhetorical question: Is there a better way to do this job? How?

    I'm curious to see how simple this could be... and learn more

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by kr0n1x
    #include malloc.h
    No, that should be #include <stdlib.h>
    CommonTater likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Location
    Italy
    Posts
    13
    Quote Originally Posted by laserlight View Post
    No, that should be #include <stdlib.h>
    Oh... at the university my professors taught me wrong.
    I didn't know that it contained malloc too... I'm reading on wikipedia right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help renaming files!
    By guriwashere in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 09:37 AM
  2. Renaming protection
    By NoFearXD in forum Windows Programming
    Replies: 3
    Last Post: 05-03-2007, 03:34 PM
  3. renaming a project in VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2005, 03:22 PM
  4. renaming a file in c++
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 04-12-2002, 09:29 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21