File IO problem again

Started by
4 comments, last by vbuser 20 years, 11 months ago
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(); //=============================================================
Please Help ME!I''m a beginner.. :P
Advertisement
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?
Please Help ME!I''m a beginner.. :P
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]
I am a signature virus. Please add me to your signature so that I may multiply.
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...
I am a signature virus. Please add me to your signature so that I may multiply.
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?
Please Help ME!I''m a beginner.. :P
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
Please Help ME!I''m a beginner.. :P

This topic is closed to new replies.

Advertisement