在C+;中访问.txt文件中的特定行+; 对于C++中的编程项目,我必须迭代通过.txt文件;但是,我只想从文件的第4行开始迭代到最后一行。我该怎么做呢
(名为“Location.txt”的.txt文件的内容) Location.txt:在C+;中访问.txt文件中的特定行+; 对于C++中的编程项目,我必须迭代通过.txt文件;但是,我只想从文件的第4行开始迭代到最后一行。我该怎么做呢,c++,C++,(名为“Location.txt”的.txt文件的内容) Location.txt: 13 5 2 5 5 1 2 2 X 7924 13 1 T 5555 5 2 Q 8753 19 4 Q 8434 8 3 P 2341 7 1 X 2523 我只想存储第四行到最后一行的值,我不知道如何跳过前三行,或者以某种方式存储这些值并删除它们 您可以按照建议使用getline,也可以使用忽略: #include <iostream> #include <fstream> #i
13 5
2 5
5 1
2 2 X 7924
13 1 T 5555
5 2 Q 8753
19 4 Q 8434
8 3 P 2341
7 1 X 2523
我只想存储第四行到最后一行的值,我不知道如何跳过前三行,或者以某种方式存储这些值并删除它们 您可以按照建议使用
getline
,也可以使用忽略
:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream ifs("Location.txt");
auto max_streamsize = std::numeric_limits<std::streamsize>::max();
int lines_to_skip = 3;
int one, two, four;
std::string three;
for (int i = 0; i < lines_to_skip; ++i)
ifs.ignore(max_streamsize, '\n');
if (ifs >> one >> two >> three >> four)
std::cout << one << "," << two << "," << three << "," << four << std::endl;
return 0;
}
向我们显示您尝试执行的操作。请显示您的最小代码。我想您可能没有使用getline()将行读入std::string(一次一行)。这就是你困惑/困难的根源。因为你有可变长度的线条,你不能直接跳到第四行。你必须读前三行。看完后简单地丢弃它们。然后,您可以根据需要从第4行开始完成读取和存储。
2,2,X,7924