对c++文件的学习

来源:互联网 发布:淘宝店铺导航css 编辑:程序博客网 时间:2024/05/16 01:21

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;


}

0 0
原创粉丝点击