Thread: Writing to file with variable filename

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Writing to file with variable filename

    Hi guys. I'm using fopen() in order to open a file to write some data. My problem is that i want to have a variable filename and not one with a constant name. I know that fopen() takes a const char as a filename parameter, but is there any way to do this?
    Thanks

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Please be a little clearer.
    Do you mean you want to change the contents of the char array the holds the name and fopen to just swich files on the fly? That ain't happening. You need to call fopen for each file you want to open and read or write.
    And since your using C++ you should use fstream object to read and write files.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Sorry... Lets say it in code way.

    Code:
    void writeFile(int i);
    
    iny main(){
    float data;
    
    for(int i=0;i<100;i++){
    writeFile(i);
    }
    
    return 0;
    
    }
    
    writeFile(int i){
    //each time this function called a new file created in the disk
    //filename : use the parameter i to construct the file name
    //for example filei.txt so to have file1.txt, file2.txt ... file100.txt
    
    
    }
    I hope this to be more clear....
    Thanks for your time.

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Then you could use the string class like this.
    Code:
    string str;
    
    str = "file" + i + ".txt;
    However if you don't want to use the string class or you're not allowed to then you can do this.
    Code:
    char str[16];
    
    sprintf("file%d.txt", i);
    I hope that clears it up for you.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Cool!!!
    Thanks a lot...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Not writing to a file.
    By sideshow_bob in forum C Programming
    Replies: 2
    Last Post: 02-19-2003, 11:25 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM