c++ - fstream skips the first character -
i trying read first 121 bytes text file onto structure.
here code.
#include <fstream.h> #include <iostream.h> #include <conio.h> #include <sys/stat.h> int main() { struct { char map[121]; } map_data; struct stat results; fstream myfile("input.txt", ios::in); myfile.read((char *)&map_data,121); if(!myfile) { cout<<"unable open file"; } if(!myfile.read((char *)&map_data,121)) { cout<<"second error occurred"; } myfile.close(); cout<<"\n here read contents of size "<<sizeof(map_data)<<"\n"; fstream outfile("output.txt", ios::out); for(int i=0;i<121;i++) { cout<<map_data.map[i]<<" "; } outfile.write((char *)&map_data,121); outfile.close(); stat("input.txt",&results); cout<<"\n size of input.txt "<<results.st_size; stat("output.txt",&results); cout<<"\n size of output.txt "<<results.st_size; getch(); }
the problem above code skips first character of file i.e h of hello. cout , output.txt file both show thing.
can guide me how solve this?
i followed guide courses.cs.vt.edu/cs2604/fall02/binio.html
the example shows 2 different ways read file notice ... in between example.
it says // same effect above
so comment out either of 2 read call.
fstream myfile("input.txt", ios::in); //myfile.read((char *)&map_data,121); if(!myfile.read((char *)&map_data,121)) { cout<<"second error occurred"; }
Comments
Post a Comment