File IO problem again
I''m tring save a structure into bin file.... but I getting problem when loading a file...
the ouput is incorrectly and sometime give strange symbol.
I made the saving data code like this:
struct Record
{
int intphone ;
char strname[50] ;
};
Record rcdUser;
//Getting information from user
//============================================================
cout <<"Enter phone:" << endl;
cin >> rcdUser.intphone;
cout <<"Enter name:" << endl;
cin >> rcdUser.strname;
//============================================================
//Saving data into a file here:
//============================================================ ofstream fout("input.dat", ios::binary);
fout.write((char *)(&rcdUser),sizeof(rcdUser));
fout.close();
//============================================================
and loading codes like these
struct Record
{
int intphone ;
char strname[50] ;
};
Record rcdUser;
//Here loading file
===============================================================
ifstream fin("input.dat",ios::binary);
fin.read((char *)(&rcdUser),sizeof(rcdUser));
cout << rcdUser.strname << endl;
cout << rcdUser.intphone << endl;
fin.close();
//=============================================================
I''ve made another testing program..
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
void main()
{
char Str1[] = "This is XXX!";
ofstream fout ("int.dat",ios::binary);
fout.write ((char *)(&Str1),sizeof(Str1));
fout.close();
}
it seems not saving anything into a file. cuz the file size is 0 byte..
why this happening?
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
void main()
{
char Str1[] = "This is XXX!";
ofstream fout ("int.dat",ios::binary);
fout.write ((char *)(&Str1),sizeof(Str1));
fout.close();
}
it seems not saving anything into a file. cuz the file size is 0 byte..
why this happening?
the first code example worked perfectly on my computer...(havent tested the second one)...
edit:> tried the second one, and it also worked..
[edited by - pag on May 26, 2003 4:44:40 AM]
edit:> tried the second one, and it also worked..
[edited by - pag on May 26, 2003 4:44:40 AM]
hmm... it cant be that you want the data in the file to be..
example:>
555-6236 282
Someone Someoneslastname
?if so you should use ios::out and ios::in for writing and reading text...
example:>
555-6236 282
Someone Someoneslastname
?if so you should use ios::out and ios::in for writing and reading text...
both first and second code not working on my PC.... strange symbol comes from the screen......
and I''m using dev C++ ..
is that a compiler bug or something?
and I''m using dev C++ ..
is that a compiler bug or something?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement