C++ 标准IO库 - 文件流操作【ifstream ofstream fstream】
2013-05-31 17:17
113 查看
本文部分参考《C++ Primer》
作者考虑不周之处,欢迎批评指正!O(∩_∩)O谢谢~
Louis.Wang
2013.5.31
*****************************************************
首先,fstream头文件定义了三种支持文件IO的类型:
(1)ifstream : 由istream派生而来,提供文件的读操作;
(2)ofstream :由ostream派生而来,提供文件的写操作;
(3)fstream : 由iostream派生而来,提供文件的读、写操作。
作者考虑不周之处,欢迎批评指正!O(∩_∩)O谢谢~
Louis.Wang
2013.5.31
*****************************************************
首先,fstream头文件定义了三种支持文件IO的类型:
(1)ifstream : 由istream派生而来,提供文件的读操作;
(2)ofstream :由ostream派生而来,提供文件的写操作;
(3)fstream : 由iostream派生而来,提供文件的读、写操作。
给定一个文件来读,在文件的末尾写一个新行,该行包括每一行的开头相对文件头的偏移量(不必写第一行的偏移量)。
#include "stdafx.h" #include <iostream> //#include "class.h" #include <fstream> #include <string> //getline包含在string头文件里 int _tmain(int argc, _TCHAR* argv[]) { //A a; //B b; //a.print(); //b.print(); fstream inOut("F:\\C++Excise\\C++Excise\\class\\Debug\\copyOut.txt", fstream::ate | fstream::in | fstream::out ); if (!inOut) { cerr << "Unable to open file! "<<endl; return EXIT_FAILURE; } fstream::pos_type endmark = inOut.tellg(); //输入流中当前位置 inOut.seekg(0,inOut.beg); //重新定位到文件头 int cnt = 0; string line; while ( inOut && inOut.tellg() != endmark && getline(inOut,line) // 取出第一行 ) { cnt += line.size() +1 ; ifstream::pos_type mark = inOut.tellg(); //get输入流 当前位置 inOut.seekp(0,inOut.end); //定位到文件末尾,向inOut 里面输入 每行开头的相对位置 inOut << cnt; if(mark != endmark) inOut << " "; inOut.seekg(mark); } inOut.clear(); inOut.seekp(0,inOut.end); inOut << "\n"; return 0; }
相关文章推荐
- C++语法基础--标准IO库--文件的输入和输出(fstream,ifstream,ofstream)
- 嵌入式 c++对文件流操作--C++文件操作详解(ifstream、ofstream、fstream)
- c++ ofstream & ifstream文件流操作
- c++ ofstream & ifstream文件流操作
- C++ 输入输出文件流(ifstream&ofstream)
- 从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态
- 从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态
- C++ 文件操作(ftream ofstream ifstream)
- C++文件读写操作(ofstream,ifstream,fstream)
- fstream提供了三个类,用来实现c++对文件的操作。(文件的创建、读、写)。 ifstream -- 从已有的文件读 ofstream -- 向文件写内容 fstream - 打开文件供读写
- 从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态 .
- C++ 标准文件的写入读出(ifstream,ofstream)
- 从零开始学C++之IO流类库(二):文件流(fstream, ifstream, ofstream)的打开关闭、流状态
- C++中string类及文件流类(ofstream,ifstream)的基本操作---按行读取文档
- 转自【北漂民工】C++文件读取操作的全部函数讲解fstream(ofstream/ifstream)
- 考考你的基础知识:C++ 文件操作ofstream、ifstream使用
- C++ ofstream和ifstream详细用法 及文件操作 导入到EXCEL
- c++ ifstream ofstream 文件流
- C++文件读写详解(ofstream,ifstream,fstream)
- C++文件读写详解(ofstream,ifstream,fstream)