文件操作:
1 引入 #include <fstream>
2 引入std using namespace std;
3 定义 ofstream ifstream
4 关联文件 ofile.open("text.txt");
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
void start_file_stream_text_fun();
int main()
{
cout << "Start main funtion!" << endl;
start_file_stream_text_fun();
system("pause"); // 防止运行后自动退出,需头文件stdlib.h
return 0;
}
void start_file_stream_text_fun() {
cout << "start_file_stream_text_fun" << endl;
ofstream ofile;
ofile.open("text.txt");
ofile << "hello world!!!" << endl;
ofile << "This is the second line!" << endl;
ofile << "This is the thired line!" << endl;
ofile.close();
ifstream ifile;
ifile.open("text.txt");
char arr_in[100] = {};
while(ifile.good()) { // 判断是否到结尾
ifile.getline(arr_in, 100);
cout << arr_in << endl;
}
if(ifile.eof()) { // 判断是否到结尾
cout << "reatch end!" << endl;
}
}