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!!