list1716.cpp

来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 32 行

CPP
32
字号
//Listing 17.16 Opening Files for Read and Write
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
   char fileName[80];
   char buffer[255];    // for user input
   cout << "File name: ";
   cin >> fileName;

   ofstream fout(fileName);  // open for writing
   fout << "This line written directly to the file...\n";
   cout << "Enter text for the file: ";
   cin.ignore(1,'\n');  // eat the newline after the file name
   cin.getline(buffer,255);  // get the user's input
   fout << buffer << "\n";   // and write it to the file
   fout.close();             // close the file, ready for reopen

   ifstream fin(fileName);    // reopen for reading
   cout << "Here's the contents of the file:\n";
   char ch;
   while (fin.get(ch))
      cout << ch;

   cout << "\n***End of file contents.***\n";

   fin.close();            // always pays to be tidy
   return 0;
}

⌨️ 快捷键说明

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