Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <chrono>
- struct Temp
- {
- int id;
- double temperature;
- char desc[20];
- };
- int main()
- {
- const int tempsSize{ 3 };
- Temp temps[tempsSize]{
- {1, 0,"ice"},
- {2, 100.0, "boil"},
- {3, 26.6, "comfort"}
- };
- std::fstream iofile;
- iofile.open("d:\\temp.dat", std::ios::trunc | std::ios::out | std::ios::in | std::ios::binary);
- if ( !iofile)
- {
- std::cout << "IOERROR: can't create/open file!\n";
- return 1;
- }
- for (int i{ 0 }; i < tempsSize; ++i)
- {
- iofile.write((char*)&(temps[i].id), sizeof(int));
- iofile.write((char*)&(temps[i].temperature), sizeof(double));
- iofile.write((char*)&(temps[i].desc), 20);
- }
- const int recordSize{ sizeof(int) + sizeof(double) + 20 };
- iofile.seekg(recordSize * 1, std::ios::beg);
- Temp fix{ 4, 100500.424242, "kipitok" };
- iofile.write((char*)&(fix.id), sizeof(int));
- iofile.write((char*)&(fix.temperature), sizeof(double));
- iofile.write((char*)&(fix.desc), 20);
- iofile.seekg(0, std::ios::beg);
- Temp readTemps[tempsSize]{};
- int idx{ 0 };
- while (!iofile.eof())
- {
- iofile.read((char*)&(readTemps[idx].id), sizeof(int));
- iofile.read((char*)&(readTemps[idx].temperature), sizeof(double));
- iofile.read((char*)&(readTemps[idx].desc), 20);
- if (iofile.fail())
- {
- break;
- }
- std::cout << "Temps #" << readTemps[idx].id << ' '
- << readTemps[idx].temperature << ' '
- << readTemps[idx].desc << '\n';
- ++idx;
- }
- iofile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement