C ++ fstream函数读取行而不提取? [英] C++ fstream function that reads a line without extracting?
本文介绍了C ++ fstream函数读取行而不提取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C ++中,fstream库(或任何库)中是否有一个函数可以让我读取行号为'\ n'的定界符而无需提取?
In C++, is there a function in the fstream library (or any library) that allows me to read a line to a delimiter of '\n' without extracting?
我知道peek()函数允许程序在不提取信息的情况下窥视"其读入的下一个字符,但我需要类似peek()的函数来执行此操作,但需要整行.
I know the peek() function allows the program to 'peek' at the next character its reading in without extracting but I need a peek() like function that does that but for a whole line.
推荐答案
您可以将getline
,tellg
和seekg
组合使用.
You can do this with a combination of getline
, tellg
and 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;
}
这篇关于C ++ fstream函数读取行而不提取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文