Thread: Options for writing to files

  1. #1
    Registered User
    Join Date
    Jul 2003

    Question Options for writing to files

    I've got 4 c++ books and a reference book, but they still leave a lot to be desired. For some reason, I/O is just not covered very well. I've also been googling but haven't found all the answers I need yet.

    I have a structure with 2 char arrays and an integer. What I'm trying to do is learn all the ways this structure can be written to a file.

    I first tried the following example without the 'endl' commands but it wrote all the data on one line. I had expected ofstream to read and include the null terminators in the char arrays, but it doesn't? So I added them myself and it seemed to work fine.
    outfile << rec.first << endl << rec.second << endl << rec.num;
    I've also read some about write() and put(). Are these the only 3 methods available?

    And could you give me a small example of how I would write the structure with write() and put() and which one of the 3 is preferable in various situations? Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Iostream methods

    If your trying to write object to file that means to serialization, check out this page. Hope it helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM