1,在目录下example.txt写入两句诗
#include <iostream>
#include<fstream>using namespace std;
int main () {
ofstream examplefile ("example.txt");
if (examplefile.is_open()) {
examplefile << "曾经沧海难为水.\n";
examplefile << "除却巫山不是云.\n";
examplefile.close();
}
return 0;}
2,把example.txt的两句诗读出来
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main () {
char buffer[256];
ifstream examplefile ("example.txt");
if (! examplefile.is_open())
{ cout << "Error opening file"; exit (1); }
while (! examplefile.eof() ) {
examplefile.getline (buffer,100);
cout << buffer << endl;
}
return 0;
}