Rename C Program - Trouble getting values into rename function

This is a discussion on Rename C Program - Trouble getting values into rename function within the C Programming forums, part of the General Programming Boards category; I'm working on a C program to batch rename files in reverse order. To make it easy I'm hard coding ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Rename C Program - Trouble getting values into rename function

    I'm working on a C program to batch rename files in reverse order. To make it easy I'm hard coding the file names - using a loop - instead of using C to read the directory. My problem is getting the "Count Up" printf values into the "old_name" variable of the rename function. Same thing with the "Count Down" printf value for "new_name".


    Code:
    #include <stdio.h>
    #define PREFIX "_MG_"
    #define EXT ".JPG"
    
    int main()
    
    {
    
    	int start_suffix = 7398, end_suffix = 8199, suffix = 9010;
    
    	while (start_suffix <= end_suffix ) {
    
    	// Count Up
    	printf("%s%04d%s",PREFIX, start_suffix, EXT);
    	start_suffix++;
    
    	// Count Down
    	printf("%s%04d%s\n",PREFIX, suffix, EXT);
    	suffix--;
    
    	rename("old_name", "new_name");
    	}
    	return 0;
    }
    The print output is exactly what I want for the renaming of the files. Now if I just could get those values into the rename function I think my program will be OK. Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You can use sprintf to write to a string, just like you use printf to print to the screen. You just have to have a way of making sure the string written to is always big enough to hold the result.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Thanks King Mir ....... I am looking at sprintf in my book and that looks like the solution. It's late now and I will try it tomorrow and post back with the results. Thanks

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a bit of code I use to name an unknown amount of output files:

    filename[] is a char array, with the first 5 letters being "Quest". Then the appropriate
    filenumber (an unsigned long int), is appended to that name to make:

    Quest1
    Quest2
    ...
    Quest770

    That's as high as I have used it, to date - no problems.

    Code:
    
     //close this file, open a new file
                            fclose(out);
                            ++filenum;
                            ultoa(filenum, afilenum, 10);
                            strcat(filename, afilenum);
    
                            if((out == fopen(filename, "wt")) == NULL) {
                              printf("\nUnable to open file: %s ", filename);
                              printf("\n Program Terminated - Press Enter When Ready");
                              gar = getchar();
                            }
                            filename[5] = '\0';

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Still having problems. I get this compiling error:

    14: error: incompatible types in assignment
    18: error: incompatible types in assignment


    And I still can't figure out the correct syntax for passing variables into the rename function.

    Code:
    #include <stdio.h>
    #define PREFIX "_MG_"
    #define EXT ".JPG"
    
    int main()
    
    {
    	int start_suffix = 7398, end_suffix = 8199, suffix = 9010;
    	char old_name[15], new_name[15];
    
    	while (start_suffix <= end_suffix ) {
    
    	// Old file name - Count up
    	old_name = sprintf("%s%04d%s",PREFIX, start_suffix, EXT);
    	start_suffix++;
    
    	// New file name - Count down
    	new_name = sprintf("%s%04d%s\n",PREFIX, suffix, EXT);
    	suffix--;
    
    	rename("old_name", "new_name");
    	}
    	return 0;
    }

    Adak ..... I don't see the rename function in your code. Is there another C function that renames files? Perhaps ultoa or strcat?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you can never assign anything ever to an array name. You should look up sprintf again, and notice where the answer is (hint: not the return value).

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Ok .......Thanks for all the replies. I decide to code it in PHP and run it from the terminal and it works as I wanted it to.





    ----------
    Len

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

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