使用ofstream和ifstream
2018-03-24 04:43
225 查看
头文件iostream预先定义了一个名为cout的ostream对象, 而要使用ofstream需要用户自己声明一个对象。这个对象和cout的用法非常相似,只是cout将结果打印到屏幕上,ofstream的对象将结果写到文件里。
ofstream outFile; outFile.open("example.txt"); //associate with a file outFile<<fixed; //Set the floatfield format flag for the str stream to fixed. outFile.precision(4); //set floatfield precision to 4 places outFile<<"This is an ofstream example file!"<<endl; outFile.close();类似的ifstream类的对象用法和cin相似,只是需注意文件是否被成功打开,可用该对象下的is_open()方法确认。
ifstream inFile; inFile.open("example.txt"); if(!inFile.is_open()){ exit(EXIT_FAILURE); //terminate the programm }
相关文章推荐
- 使用fstream,ifstream,ofstream读写文件的一种情形
- fstream(ifstream和ofstream)使用详解
- ofstream和ifstream
- 解析ifstream 与ofstream的用法
- fstream ifstream ofstream的使用
- ifstream与ofstream
- 解析ifstream 与ofstream的用法
- fstream、ifstream、ofstream创建新文件
- JAVA 8 的Stream API操作,别再使用if-else循环了,
- ofstream和ifstream详细用法
- C++中ifstream使用笔记(一)(常用方法和注意事项)
- C++的ofstream与ifstream使用
- 考考你的基础知识:C++ 文件操作ofstream、ifstream使用
- 解析ifstream 与ofstream的用法
- ofstream 和 ifstream 用法
- ofstream,ifstream,fstream之ios::ate,ios::app,ios::in,ios::out
- ofstream和ifstream详细用法
- ofstream和ifstream详细用法
- 四种读写文件的方式:系统调用(open/read/write),C语言(fopen,fgets, fputs),C++(ifstream, ofstream getline,)泛型算法
- ofstream 和ifstream的具体用法