从文件读取时如何使用运算符后将光标移动到下一行">>"
How to get cursor to next line after using ">>" operator when reading from file
我正在尝试从一个.txt文件中读取信息,如下所示。要读取前2行整数,我使用">>"运算符将它们读取到一个数组中。我的问题是,我想读取字符串的下一行(全部),这样我就可以将其转换为流并解析它,然而,当我尝试简单地使用getline时,它实际上并没有读取字符串中的任何内容,这让我认为光标实际上并没有移动到下一行,我想知道如何执行此操作或任何其他可以用于保存目的的方法。txt文件的结构如下:
2
10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
765DEF 01:01:05:59 enter 17
ABC123 01:01:06:01 enter 17
765DEF 01:01:07:00 exit 95
ABC123 01:01:08:03 exit 95
我的代码如下所示:
#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
using namespace std;
int main()
{
int arr[24];
int milemarker;
int numberofCases;
ifstream File;
File.open("input.txt");
File >> numberofCases;
for (int i = 0; i < 24; i++)
{
File >> arr[i];
}
for (int i = 0; i < 24; i++)
{
cout << arr[i] << " ";
}
cout << endl;
string line;
getline(File, line);
cout << line;
system("pause");
}
我想你错过了getline()
呼叫:
#include<iostream>
#include<fstream>
#include<string>
#include <sstream>
using namespace std;
int main()
{
int arr[24];
int milemarker;
int numberofCases;
ifstream File;
File.open("input.txt");
File >> numberofCases;
for (int i = 0; i < 24; i++)
{
File >> arr[i];
}
for (int i = 0; i < 24; i++)
{
cout << arr[i] << " ";
}
cout << endl;
string line;
getline(File, line);
getline(File, line);
cout << line;
system("pause");
}
运算符>>
读取分隔符之间的标记。默认情况下,空格和新行是分隔符。所以在第一个循环中的最后一个运算符>>
调用之后,您仍然在同一行上,并且第一个getline()
调用只读取新行字符。
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 从C++dll访问C#中的一行主要参数
- 在C++中,我如何接受不同于同一行的用户输入
- 如何从一行中读取特定数量的字符?(C++)
- EASTL矢量<向量<int>>连续的
- 读取最后一行代码算法 - c++ 时出现问题
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 有没有办法在一行中填充矢量图
- 需要将一行代码从C++ Qt翻译成PyQt
- 为什么如果我添加这一行,我的程序会不断询问值
- 如何使代码打印文本文件中的第一行?
- 如何在C++中以这种方式返回一行文本?
- 如何使用运算符在同一行中多次调用函数
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 而(!inputfile.eof())只读取第一行?
- 这段代码的最后一行在做什么?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- C++找出覆盖同一行的数组