Archived

This topic is now archived and is closed to further replies.

vbuser

File IO problem again

Recommended Posts

vbuser    122
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(); //=============================================================

Share this post


Link to post
Share on other sites
vbuser    122
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?

Share this post


Link to post
Share on other sites
pag    100
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]

Share this post


Link to post
Share on other sites
pag    100
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...

Share this post


Link to post
Share on other sites
vbuser    122
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?

Share this post


Link to post
Share on other sites
vbuser    122
I was using cout to show data from the first structure member after loading a file.

And the same thing in second program...

just can''t get a correct output from the screen after loading the .dat files

Share this post


Link to post
Share on other sites