Problem with Binary File I/O

This is a discussion on Problem with Binary File I/O within the C++ Programming forums, part of the General Programming Boards category; Writing goes fine, evrythin in the binary file is just the way it should be, only problem i am havin ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    87

    Problem with Binary File I/O

    Writing goes fine, evrythin in the binary file is just the way it should be, only problem i am havin is with reading. At one point my code will just start to spurt strange BS.

    Code:
                    Book* New;
    
    	DB_in.open("db.raw", ios::in || ios::binary);
    
    	for(int i = 0 ; i < SizeOfDB; i++)
    	{
    		if(!FreeStack.ElementOfFreeStack(i))
    		{
    			New = new Book;
    
    			DB_in.seekg((i * sizeof(Book)), ios::beg);
    			DB_in.read((char *)New, sizeof(Book));
    
    			Main->Insert(New);
    		}
    	}
    think the problem is in my logic up there... SizeOfDB is the total number of books in the binary file. Tryed just runnin along trying to get the next book every time but that didn't work so i started to jump from the start everytime. Kinda hard for me to post all of the code cause there is simply to much but kinda limited the problem to the code above using the debugger.

    If you see any obvious mistakes or got suggestions on what i should do/try plz lemme know, kinda n00b at binary file i/o.

    Thnx in advance

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    donnu if it helps but here is the code i used to insert:

    Code:
    	DB_out.open("db.raw", ios::binary || ios::ate);
    
    	DB_out.seekp( ( ( Position * sizeof(Book) )), ios::beg );
    	DB_out.write( (char *)book, sizeof(Book) );
    
    	DB_out.close();

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) You never check if the file opened properly (ReadFile.fail() or ReadFile.good()).
    2) You don't need the seekg in the reading. The file pointer automatically repositions after a read.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > ios::in || ios::binary
    This is not the same as
    ios::in | ios::binary
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    87
    Alrite thnx a lot problem fixed, worked fine when i changed
    Code:
    ios::in || ios::binary
    to
    Code:
    ios::in | ios::binary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. File I/O problem
    By dacbo in forum C Programming
    Replies: 4
    Last Post: 01-17-2006, 07:22 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

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