Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- //ofstream - output stream
- //ifstream - input stream
- //fstream - input/output stream
- // std::ios::app
- // std::ios::trunc
- // std::ios::out
- // std::ios::in
- // std::ios::binary
- struct Temp
- {
- int id;
- float 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::ofstream outf;
- outf.open("d:\\file.txt", std::ios::trunc | std::ios::out);
- if ( !outf )
- {
- std::cout << "IOERROR: can't create/open file!\n";
- return 1;
- }
- for (int i{ 0 }; i < tempsSize; ++i)
- {
- //outf << temps[i].id << ' ' << temps[i].temperature << ' ' << temps[i].desc << '\n';
- outf << temps[i].id << '\n' << temps[i].temperature << '\n' << temps[i].desc << '\n';
- }
- outf.close();
- int idx{ 0 };
- Temp readTemps[tempsSize * 3];
- std::ifstream inf;
- inf.open("d:\\file.txt");
- if (!inf)
- {
- std::cout << "IOERROR: can't open file!\n";
- return 1;
- }
- while (!inf.eof())
- {
- inf >> readTemps[idx].id >> readTemps[idx].temperature >> readTemps[idx].desc;
- if (inf.fail())
- {
- if (inf.eof())
- {
- break;
- }
- inf.clear();
- inf.ignore(100, '\n');
- }
- std::cout << "Temps #" << readTemps[idx].id << ' '
- << readTemps[idx].temperature << ' '
- << readTemps[idx].desc << '\n';
- idx++;
- }
- inf.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement