Thread: incrementing file names

  1. #1
    Electrical Engineer
    Join Date
    Feb 2005
    Posts
    3

    incrementing file names

    I'm looking to replace a piece of code that writes and image file (bmp) with code to write a series of images, image1,image2,image3,image4...etc instead of overwriting the same image each time.

    the file is referenced several times throughout the code, which is spread over 3 .c files which are compiled to one

    my guess is to make a string, that has a counter in it, but I'm not sure how to do that exactly...
    like

    "image#.bmp"

    where # is an integer that I increment after writing the file.

    how would I implement that?

    and would I need to do anything special so that the string could be used in more than one .c file, but be incremented in one spot.

    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
       char filename [ FILENAME_MAX ];
       int i;
       for ( i = 1; i < 5; ++i )
       {
          sprintf(filename, "image%d.bmp", i);
          puts(filename);
       }
       return 0;
    }
    
    /* my output
    image1.bmp
    image2.bmp
    image3.bmp
    image4.bmp
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Quote Originally Posted by Rogue5
    and would I need to do anything special so that the string could be used in more than one .c file, but be incremented in one spot.
    Use the keyword extern
    Here's an example:

    Code:
    /* file1.c */
    #include <stdio.h>
    
    int count = 0;
    
    int main() {
      count = 5;
      show();
    
      count = 10;
      show();
    
      return 0;
    }
    Code:
    /* file2.c */
    #include <stdio.h>
    
    void show() {
      extern int count;
    
      printf("%d\n",count);
    }
    Which will output:

    5
    10

  4. #4
    Electrical Engineer
    Join Date
    Feb 2005
    Posts
    3
    wow, I wake up this morning and all my questions are answered already
    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM