#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    fstream file variable?


    Is there a way to have the user specify what he wants the file to be called that he is creating and appending to? For example...

    string filename;
    ofstream blah;

    cout << "file name?\n";
    cin >> filename;

    blah.open("filename.txt",ios::app);

    but instead of "filename.txt" it has whatever the user typed in and then ".txt" Thanks.

    -andy
    hmmm...
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Have you tried this:
    blah.open(filename.c_str(),ios::app);
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    Well I'll be..:) Thanks man..but how do I get a .txt at the end of that baby on the same line?

    EDIT: I concactinated (however you spell that word) it with another string with a value of ".txt" It worked but im hoping i can add it on the same line as the variable.
    Last edited by andy3109; May 14th, 2003 at 06:01 PM.
    hmmm...
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Either:
    filename += ".txt";
    blah.open(filename.c_str(),ios::app);

    or:
    blah.open(string(filename + ".txt").c_str(), ios::app);

    or even:
    blah.open((filename + ".txt").c_str(), ios::app);

    Take your pick!
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    scorp..have I ever told you i loved you? (in a non-gay way)

    THANKS MAN!
    -andy
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo