Thread: Writing to a Data File

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    8

    Writing to a Data File

    For the second part of my assignment, I have to write some data to a file, but I can't find notes anywhere in my textbook or class notes that explain how to do something like this:

    "The second output requirement for this program is to save class roster in a data file. It needs to include only students names and final scores. The format should look like:

    Terri Smith 70
    Mary White 90
    Pete William 80
    Harry Lia 100
    Amy Peterson 60

    Don't forget to close the file at the end!"

    Ideas?

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    40

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    8
    Excellent, thanks a lot!!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That tutorial is out of date and uses the non-standard fstream.h header. There are a few small differences between fstream and fstream.h, so be careful.

    Do you know how to output to cout? It is almost exactly the same, except you need to create an ofstream object. For example, if you create an ofstream variable called fout, then just output your data to fout instead of cout.
    Code:
    ofstream fout("output.txt");
    >> Don't forget to close the file at the end!
    Hopefully your instructor knows that a file stream closes automatically when it goes out of scope and its destructor is invoked. There is no need to close it explicitly.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    if you wish to write a class object into file , then do the following using fstream object

    Code:
       fstream fout;
    	  fout.open("filename.ext",ios::binary|ios::out);
    	  fout.write((char *)&obj,sizeof(roster));
    	  fout.close();
    where filename.ext is any file that you wish to use. ios::binary for the write mode, and the format for writing an object to file using the write function. parameters of this fstream defined function are the object and the size.

    hope this helps you out!
    Last edited by praseodeveloper; 10-14-2005 at 12:43 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your "class" should have only POD types if writing it out that way in binary. If it has any members that use dynamic memory like std::string then it won't work.

  7. #7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM