如何让程序读取一行? C++
How to get program to read a line? C++
我有一个数据集 headers 和下面的数据 headers。如何让 C++ 读取实际数据的第一行(从第 3 行开始)并继续读取直到文件结束?
我知道您必须在某些已声明的变量上使用 while 循环和“++”,但我不确定如何使用。
这里是数据文件的截图:enter image description here
只需在 while 循环之前先将第一行读入虚拟变量
How to read line by line or a whole text file at once?
#include <fstream>
#include <string>
int main()
{
std::ifstream file("Read.txt");
std::string str;
std::getline(file, str); // read a line, as dummy read
while (std::getline(file, str)) // keep reading till end of file
{
// Process str
}
}
我有一个数据集 headers 和下面的数据 headers。如何让 C++ 读取实际数据的第一行(从第 3 行开始)并继续读取直到文件结束?
我知道您必须在某些已声明的变量上使用 while 循环和“++”,但我不确定如何使用。
这里是数据文件的截图:enter image description here
只需在 while 循环之前先将第一行读入虚拟变量
How to read line by line or a whole text file at once?
#include <fstream>
#include <string>
int main()
{
std::ifstream file("Read.txt");
std::string str;
std::getline(file, str); // read a line, as dummy read
while (std::getline(file, str)) // keep reading till end of file
{
// Process str
}
}