Hi all, I am trying to write a struct to a file then read it again, but it cant get it to work. It compiles file, but when I try to read it, I don’t get anything. I don’t think that I am writing the data to the file correctly. Because when I have a look at the file I am writing to with a hex editor I get this.
Below is the code, anyideas on what I am doing wrong?Code:f4 24 3d 00 14 25 3d 00 00 ô$=
Code:#include <cstdlib> #include <iostream> #include <fstream> using namespace std; struct ITEM { string txta; string txtb; } item; int main(int argc, char *argv[]) { item.txta = "txta"; item.txtb = "txtb"; cout << item.txta << " = " << item.txtb << endl; ofstream fout("file.dat", ios::binary); fout.write((char *)(&item), sizeof(item)); fout.close(); item.txta = ""; item.txtb = ""; ifstream fin ("file.dat"); if (fin.is_open()) { while (! fin.eof() ) { fin.read((char *)(&item),sizeof(item)); cout << item.txta << " = " << item.txtb << endl; } } else cout << "Unable to open file"; return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


