Writing Variables to Files with Filenames stored in Variables

This is a discussion on Writing Variables to Files with Filenames stored in Variables within the C++ Programming forums, part of the General Programming Boards category; Hello all. I have another quick one concerning... Yup, you guessed it! Variables and filenames. Basically, I have a questionnaire ...

  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,319
    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,319
    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, 01:53 PM
  2. Writing to files on exact spots
    By R@m0n in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 05:06 AM
  3. Write and use data stored in data files...
    By alex6852 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2001, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21