problem reading binary file

I use almost the same code to write to binary file but the problem comes when i try to read the file
here i write to file which seem to work just fine no errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct data {
string name;
string address;
string number;
};

int main()
{
  struct data pbook;


  ofstream fout("white", ios::out | ios::binary | ios::app);
  if(!fout)
 {
    cout << "Cannot open file.\n";
    return 1;
  }


do
	{
	cout << " Upper Case 'Q' to Quit Or" << endl;
	cout << " Enter persons full name : ";
	getline(cin,pbook.name);
	
	if ( pbook.name == "Q" )
			break;

	cout << "\n Enter persons address : ";
	getline(cin,pbook.address);
	cout << "\n Enter persons phone number : ";
	getline(cin,pbook.number);


  fout.write((char *) &pbook, sizeof(struct data));

	}
	while( pbook.name != "Q" );


  fout.close();

  return 0;
}

here i try to read file but only displays the very first name
then program closes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct data {
string name;
string address;
string number;
};

int main()
{
  struct data pbook;

  ifstream fin("white",ios::binary|ios::in);

  if(!fin) 
  {
    cout << "Cannot open file.\n";
    return 1;
  }

  fin.read((char *)&pbook,sizeof(struct data));
  fin.close();
  cout << pbook.name << endl;
  cout << pbook.address << endl;
  cout << pbook.number << endl;
 return 0;
}


i get an error

Infile.exe has triggered a breakpoint
HEAP[Infile.exe]: Invalid address specified to RtlValidateHeap( 00900000, 00250BB8 )
Windows has triggered a breakpoint in Infile.exe.

This may be due to a corruption of the heap, which indicates a bug in Infile.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Infile.exe has focus.

The output window may have more diagnostic information.
Infile.exe has triggered a breakpoint
The program '[8280] Infile.exe: Native' has exited with code 0 (0x0).

just so you know i didnt press f12

any help would be great!!

Last edited on
Aarrghh! Who teaches you people all this cruft?!

Stuff that would never have any hope of working:

40 fout.write((char *) &pbook, sizeof(struct data))

The string type is a class. You cannot binary dump classes to file! Did you even examine the file after writing it?

Google around "c++ serialization" for help on this subject.

There are a lot of ways of doing things. Use an INI-file construct. Use a Lisp-like or Scheme-like s-expression construct. Use fixed-size fields. Use length-data fields. Use data-delimiter fields. Use something that gives your file a decipherable format.

But, since all you are writing is string data, why use a binary file? You aren't saving any space. Here is a text-file format that simply uses one line per phonebook entry -- three lines total. (Hence, the number of items in the phonebook is the number of lines in the file divided by three.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

struct phonebook_entry_t
  {
  string name;
  string address;
  string number;
  };

ostream& operator << ( ostream& outs, const phonebook_entry_t& entry )
  {
  return outs << entry.name    << '\n'
              << entry.address << '\n'
              << entry.number  << '\n';
  }

istream& operator >> ( istream& ins, phonebook_entry_t& entry )
  {
  getline( ins, entry.name    );
  getline( ins, entry.address );
  getline( ins, entry.number  );
  return ins;
  }
A phone book file might look like this:
Anthony Archer
123 Field Rd, Virginia, 23867
555-1234
Jennifer Bennett
7 Rabbit Run Rd, Oregon, 97402
541-555-1839
Zachary Holmes
42 Pixwhey Ave, TX, 76871
979-555-7647

BTW, use good names for your objects. "data" is a bad name -- everything in the computer is 'data'. I renamed it "phonebook_entry_t" -- it is a type that indicates an entry in a phonebook. Names should be that obvious.

Hope this helps.
I am teaching myself by reading c++ primer plus 5th edition i just started the chapter on objects and classes not very far in the book if you couldnt guess
i just decided that a address book would be a good learning tool because all the stuff that could be added even when i get far enough to add grafix i can still rewrite my address book
all i have is sample code and the small examples online to compare
but by looking at your code I need to read up on outs, ins, operator <<, and operator >>

Google around "c++ serialization" for help on this subject.

thanks for the point in the right direction

this was my .txt version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct data
{
string name;
string address;
string number;
};
int main()
{

	data pbook;

	ofstream outfile;
	outfile.open("white.txt", ofstream::out | ofstream::app); 

	
	do
	{
	cout << " Upper Case 'Q' to Quit Or" << endl;
	cout << " Enter persons full name : ";
	getline(cin,pbook.name);
	
	if ( pbook.name == "Q" )
			break;

	cout << "\n Enter persons address : ";
	getline(cin,pbook.address);
	cout << "\n Enter persons phone number : ";
	getline(cin,pbook.number);
	
	
	cout << pbook.name << "  " << pbook.address << "  " << 	pbook.number << endl;
	
		
	outfile << pbook.name ;
	outfile << pbook.address;
	outfile << pbook.number << endl;
	
	
	}
	
	while ( pbook.name != "Q" );

	outfile.close();

	return 0;
}


for my infile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
const int size = 10;
int main()
{
	string line, name;


	char file[size];
	ifstream infile;

	cout << " enter file name :";
	cin.getline(file,size);
	infile.open(file);
if(!infile.is_open())
{
	cout << " could not open file" << file << endl;
	cout << " program Terminating....\n";
	exit(EXIT_FAILURE);
}

cout << " enter name of person";
cin >> name;



if(infile)
{
	while ( getline ( infile , line ))
	{
		
			line.find(name);
			if ( line == name )
			{
				cout << line;
			
			}
	}
}



infile.close();

	return 0;
}


but i couldnt figure out how to search for a name and display only that name, address, and number


There are a lot of ways of doing things. Use an INI-file construct. Use a Lisp-like or Scheme-like s-expression construct. Use fixed-size fields. Use length-data fields. Use data-delimiter fields. Use something that gives your file a decipherable format.


this lets me know i still have lots to learn!!
I am teaching myself by reading c++ primer plus


That title sounds like a cheap knock-off of the well-respected book called C++ Primer, by Lippmann (who also wrote Inside the C++ Object Model). And of course Stroustrup's book is required reading at some point, but it may not be for the programming novice.
it probably is a knock off
it is written by Stephen Prata
i was going to buy the c++ programming language by Bjarne Stroustrup after learning the basics because the web page i read said its more of a reference book

I list the book as advanced, rather than tutorial, because in my opinion it is not truly suitable for a beginner to use in trying to learn the language on their own. While the text and examples do proceed step-by-step from the simple to the complex, it is extremely dense with concepts and information. Rather than the breezy style of what I consider to be the better tutorials this book reads like a college text.
This would be an excellent book for one familiar with C, or earlier versions of C++, looking to get up to speed on all the features of the new standard C++, and it makes an excellent reference book. No serious C++ programmer should be without it, but I do not recommend it to beginners learning their first programming language.


that came from a website while researching what book to start with


do you recommend reading one of those others instead or do you think primer plus is good enough for a beginner

http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326973
here you can look inside the book to make a better judgment if you like
Topic archived. No new replies allowed.