Thread: Writing Variables to Files with Filenames stored in Variables

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Wink Writing Variables to Files with Filenames stored in Variables

    Hello all.

    I have another quick one concerning... Yup, you guessed it! Variables and filenames. Basically, I have a questionnaire that the user fills in. I have to then save the answers in a txt file. Also, the filename is stored in a string, so I wish to write the answers to an already existing txt with the same name as the variable. What is the correct syntax for this?

    Thanks
    Necrofear

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the filename is stored in a string, make sure to call c_str() when passing that to the filestream's constructor or open function.

    Are you having specific problems, or were you just referring to the syntax for using a string variable as a filename?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    No specific problems, just pondering the syntax. Also, which operator do I use to write a variable to the file?

    Something like: Stream<<"Text to save"<<variabletosave;

    Many thanks for the reply.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes, that looks correct. If you know how to output it to cout, you can probably do the exact same thing with an ofstream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to .DAT files
    By Xinco in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2006, 02:53 PM
  2. Writing to files on exact spots
    By R@m0n in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 06:06 AM
  3. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 01:45 PM
  4. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM