Thread: Help in outputing different file names in a loop

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Help in outputing different file names in a loop

    I got this program here which is suppose to output a .ppm file. How can i modify the Filename to get an output of out1.pmm, out2.pmm, out3.pmm .. so on and so forth for a while loop?

    Code:
    unsigned char Filename = "out1.ppm";
    
    while (i < 100)
    {
    
    FILE *stream;
    
    stream = fopen(Filename, "wb" );                     // Open the file for write
    
    //rest of the program;
    //..
    //..
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Code:
    #include <sstream>
    // ...
    
    std::stringstream ss;
    
    while (i < 100)
    {
      ss << "out" << i << ".ppm";
      FILE *stream;
    
      stream = fopen(ss.str().c_str(), "wb" );   // Open the file for write
      ss.str("");                                // clear the stringstream
    //rest of the program;
    //..
    //..
    
    }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if this is C++ you probably need to use fstream instead of FILE*

    If this is C - you should ask on the proper forum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    There's no such need. Iostream laid the last straw on my camel a long time ago. I use stdio.h all the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM