Sex*_*ast 10 c++ fstream extraction peek
在C++中,fstream库(或任何库)中是否有一个函数允许我在不提取的情况下将行读取到'\n'的分隔符?
我知道peek()函数允许程序"偷看"它读取的下一个字符而不提取,但是我需要一个peek()之类的函数来完成这个但是整行.
Kle*_*ist 13
你可以结合使用getline,tellg和seekg.
#include <fstream>
#include <iostream>
#include <ios>
int main () {
std::fstream fs(__FILE__);
std::string line;
// Get current position
int len = fs.tellg();
// Read line
getline(fs, line);
// Print first line in file
std::cout << "First line: " << line << std::endl;
// Return to position before "Read line".
fs.seekg(len ,std::ios_base::beg);
// Print whole file
while (getline(fs ,line)) std::cout << line << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6508 次 |
| 最近记录: |