C++ fstream getline 参数

C++ fstream getline parameters

我是 C++ 的新手,我想问一个关于如何使用 fstream 在文件中查找行的问题。 我只找到了这个,有人会向我解释这些参数的含义吗?

file.getline(char *,int sz);

谢谢

直接来自here

第一个变量:

Pointer to an array of characters where extracted characters are stored as a c-string.

第二个变量:

Maximum number of characters to write to s (including the terminating null character). If the function stops reading because this limit is reached without finding the delimiting character, the failbit internal flag is set. streamsize is a signed integral type.

如果你的意思是std::basic_stream::getline(),你提供了一个指向字符数组的指针和该数组的大小。你必须自己在某个地方创建数组。如果某行比 sz - 1 长,则只读取长度为 sz - 1 的部分。

如果您不知道输入文件中的最大行长度,最好使用std::getline(),例如:

std::string line;
std::getline(file, line);