Advertisement
junghu1124

Untitled

Sep 15th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void write() {
  7.     ofstream ofs("data.txt"); // RAII 패턴을 적용하고 있어 리소스 해제가 스택을 통해 이루어짐
  8.     ofs << 0.0 << ' ' << -0.4 << '\n';
  9.     ofs << -0.5 << ' ' << 0.2 << '\n';
  10.     ofs << 0.5 << ' ' << 0.8 << flush;
  11. }
  12.  
  13. void read() {
  14.     ifstream ifs2("data.txt");
  15.     float val2;
  16.     cout << boolalpha;
  17.     while (ifs2 >> val2) {
  18.         cout << val2 << endl;
  19.         cout << ifs2.fail() << ' ' << ifs2.eof() << endl;
  20.     }
  21.     cout << ifs2.fail() << ' ' << ifs2.eof() << endl;
  22. }
  23.  
  24. int main() {
  25.     write();
  26.     read();
  27.     return 0;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement