Thread: Using variables in fstream

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Question Using variables in fstream

    does anybody know how to use a variable when declaring an fstream file? Here's what I want to do:

    //this code is just an example, not a problem
    for (x=0; x <10;x++)
    {
    ofstream file("file(x).txt");
    file<<"Hi, peeps!";
    file.close();
    }

    this code would create ten files, each named file(x) (x being a number 1-10).

    How can I do this?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    you need to change x to a string using itoa() --it's in coni0.h-- if you have it or sprintf. Then append to a string with the file name and then append the file extension. Do your file thingy. Then reset the initial string. Something like this:

    char fileName[18] = "file";
    char addOn[3];
    char extension[5] = ".txt:;
    for (x=0; x <10;x++)
    {
    itoa(x, addOn, 10);
    strcat(fileName, addOn);
    strcat(fileName, extension)
    ofstream file(fileName);
    file<<"Hi, peeps!";
    file.close();
    strcpy(fileName, "file");
    }

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    a (slightly easier to read) version of this would be as follows (the principle is exactly the same, this just takes fewer commands):
    Code:
    char fileName[20];
    for (x = 0; x < 10; x++){
    	sprintf((char *)&fileName,"file%d.txt",x);
    	ofstream file(fileName);
    	file<<"Hi, peeps!";
    	file.close();
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or without the cast (and incorrect use of &)
    &nbsp; sprintf(fileName,"file%d.txt",x);

    &fileName has a type of char (*)[20] (pointer to an array), which is why you needed the cast.
    fileName has a type of char *(pointer to the first element), which is what sprintf (and most string functions in general) expect.
    http://www.eskimo.com/~scs/C-faq/s6.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I have conio.h, but my compiler still game me an error on itoa.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  5. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM