C++讀/寫文件定位到某行


僅適用於每行的長度相同

C++中seekp()和seekg()函數功能

seekp:設置輸出文件流的文件流指針位置
seekg:設置輸入文件流的文件流指針位置
函數原型:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg( streamoff off, ios::seek_dir dir );
函數參數
pos:新的文件流指針位置值
off:需要偏移的值
dir:搜索的起始位置
dir參數用於對文件流指針的定位操作上,代表搜索的起始位置
在ios中定義的枚舉類型:
enum seek_dir {beg, cur, end};
每個枚舉常量的含義:
ios::beg:文件流的起始位置
ios::cur:文件流的當前位置
ios::end:文件流的結束位置

1 #include<fstream>
2 #include<iostream>
3 #include<string>
4 string a;
5 ifstream infile2("C:\\filename.csv");
6 infile2.seekg(22 * (i+1));//每行的長度為22個字符
7 getline(infile2,a);

 轉載自http://blog.sina.com.cn/s/blog_679f85d40100mysi.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM