Thread: Very quick question about File I/O

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Very quick question about File I/O

    I have my program successfully writing to text files but when the program starts again, it deletes the text file and begins writing it fresh. I thought a solution would be to have the user input the file name they would like the program to create but I cannot figure out the correct syntax.

    Code:
    cout << "What do you want to call your file? eg. myfile.txt";
    char filename[256];
    cin >> filename;
    ofstream flout('filename');
    any help would be appreciated.
    Thanks!

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    cout << "What do you want to call your file? eg. myfile.txt";
    char filename[256];
    cin >> filename;
    ofstream flout(filename,ios::app);
    that should do the trick
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I'd highly recommend:
    Code:
    cout << "What do you want to call your file? eg. myfile.txt";
    string filename;
    cin >> filename;
    ofstream flout(filename.c_str());
    To use the char[256] like you showed, you just need to remove the quotes around filename when creating the ofstream. However, I just happened to read in Josuttis' The C++ Standard Library that you should do this to avoid overwriting the char array:
    Code:
    cout << "What do you want to call your file? eg. myfile.txt";
    char filename[256];
    cin >> setw(sizeof(filename)) >> filename;
    ofstream flout(filename);
    EDIT: Didn't see major_small's post but I still think that the single quotes is the specific problem. The ios::app will let you append instead of overwrite if the chosen file already exists. And I still recommend the string instead of char[].
    Last edited by jlou; 09-09-2003 at 07:32 PM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, that's his problem if he wants the user to enter the filename, but the main problem was that the program would overwrite the file... this way it just adds to the end of the file..

    ios::nocreate //doesn't open file if it's not already there
    ios::app //adds to end of file
    ios::trunc //overwrites file
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    Thanks alot guys.
    I had filename without any quotes when I first created it but for some reason it wouldn't compile :/
    Screwy MSVC++

    oh, but I forgot to add that it does work now

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Josuttis' The C++ Standard Library
    Good book
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  2. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. C++ File I/O question
    By zero_cool in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2005, 10:43 AM
  5. File I/O on Network drive, Quick Question
    By kransk in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2003, 09:17 AM