1. 程式人生 > >文字檔案的讀寫

文字檔案的讀寫

#include <iostream>
#include <fstream>

using namespace std;

int main() {
	ofstream out("1.txt");
	if (out.is_open()) {
		out << "this is a line" << endl;
		out << "hello world" << endl;
	}
	out.close();
	
	ifstream in("1.txt");
	if (!in.is_open()) {
		cerr << "open file error" << endl;
		exit(1);
	}
	char buffer[256];
	while (!in.eof()) {
		in.getline(buffer, 100);
		cout << buffer << endl;
	}
	in.close();
	return 0;
}