僅適用於每行的長度相同
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);