C++ 读写文件 实例
2012-02-03 15:57
232 查看
按行写文件:
按行读文件:
#include <fstream> #include <iostream> using namespace std; int main() { ofstream outfile("output.txt", ios::out); if(outfile.is_open() == true) { outfile << "first line!" << endl; outfile << "second line!" << endl; outfile << "last line!" << endl; } outfile.close(); return 0; }
按行读文件:
#include <fstream> #include <iostream> #include <string> using namespace std; int main() { ifstream infile("output.txt", ios::in); if(infile.is_open() == true) { string line; while(infile.eof() == false) { getline(infile, line); cout << line << endl; } } infile.close(); return 0; }
相关文章推荐
- C++ 读写文件实例总结 (StringToken 为单位读取 以及char为单位读取)
- 【C++】【一日一练】读写文件小实例【20140510】
- C/C++读写文件实例
- C++ 读写文件安全又简洁的简单实例
- C++读写INI配置文件的类实例
- C++文件简单的读写操作实例
- C++文件读写(一):fstream
- C++读写EXCEL文件方式比较
- C++中头文件和类的实例的大小之间的关系
- C++读写文件
- C/C++文件IO读写
- C++ stl 读写ini配置文件
- (转载)你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
- C++中结构体数据的文件读写
- C++读写ini配置文件
- C++ 文件操作实例
- C++中对文件进行读写操作
- C++ 读写文件笔记
- C/C++常用读写文件方法
- C++文件读写操作