⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filestream.cpp

📁 C++高级编程这本书所附的源代码
💻 CPP
字号:
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv)
{
  ofstream fout("test.out");

  // 1. output the string "12345"
  fout << "12345";

  // 2. verify that the marker is at the end
  long curPos = fout.tellp();
  if (curPos == 5) {
    cout << "Test passed: Currently at position 5" << endl;
  } else {
    cout << "Test failed: Not at position 5" << endl;
  }

  // 3. move to position 2 in the stream
  fout.seekp(2, ios_base::beg);

  // 4. output a 0 in position 2
  fout << 0;
  fout.flush();

  // 5. open an input stream on test.out
  ifstream fin("test.out");

  // 6. read the first token as an integer
  int testVal;
  fin >> testVal;

  // 7. confirm that the value is 12045
  if (testVal == 12045) {
    cout << "Test passed: Value is 12045" << endl;
  } else {
    cout << "Test failed: Value is not 12045 (it was " << testVal << ")" << endl;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -