Zapis struktur danych do pliku
Ostatnio zmodyfikowano 2011-08-22 10:53
mykosik Temat założony przez niniejszego użytkownika |
» 2011-08-22 10:53:56 OK , skapowałem.W celu łatwiejszego zrozumienia zamieniłem referencję & data na & myStruct oraz zamieniłem plik test.dat na test.txt i od razu wszystko zadziałało (mam nadzieję ,że jest to poprawnie jak nie, to proszę o uwagi) Ostatecznie #include <iostream> #include <string> #include <fstream> using namespace std;
struct s { int a; float b; };
void write( const std::string & file_name, s & myStruct ) { std::ofstream out( file_name.c_str() ); out.write( reinterpret_cast < char *>( & myStruct ), sizeof( s ) ); }
void read( const std::string & file_name, s & myStruct ) { std::ifstream in( file_name.c_str() ); in.read( reinterpret_cast < char *>( & myStruct ), sizeof( s ) ); string liczbac; cout << myStruct.a << endl; cout << myStruct.b << endl; }
int main() { s myStruct; myStruct.a = 5; myStruct.b = 34; read( "test.txt", myStruct ); write( "test.txt", myStruct ); }
|
|
1 « 2 » |