读取文件并解析输入
reading in file and parsing the input
所以我有一个文件,其中前3行将始终引用"状态",其余将引用"转换"。我知道前3行始终采用相同的格式,并且在同一行的输入之间使用制表符,但"转换"的数量未知,但始终从第4行开始。
所以我通过处理"状态"
for(int i = 0; i < 3; i++){
myFile >> junk;
myFile >> stateNum;
myFile >> designation;
//make object and put in container
}
现在我需要处理"转换",所以如果我的输入文件看起来像
state 1 a
state 2 b
state 3 c
trans a b c d &
trans d s i & 3
...
trans 4 e & d g
在同一行上的所有空白都是一个选项卡的情况下,state/trans进入junk
,因为我知道1-3是state,其余的是transition,所以不需要它们。我现在如何从第4行的开始并继续到文件末尾?基本上,我使用什么if
条件来实现
if(???){
myFile >> junk;
myFile >> one;
myFile >> two;
...
myFile >> five;
//create object and place in container
}
其中一、二、,。。五个早些时候宣布,一个,。。。,五个对应于"trans"之后的5个条目
尝试放置
myFile >> junk;
myFile >> stateNum;
myFile >> designation;
在循环之前,然后读取循环中的每个转换,这是一个while循环,因为你不知道会有多少。
正如Dieter所说,您应该使用字符串流和getline
string line;
while (getline (myFile, line)){
//this will loop until the end of the file
stringstream line_stream(line);
string word;
while (line >> word){
//this will loop until there are words (transitions) on the line)
//(your code)
}
相关文章:
- 用c++从输入文件中读取另一行
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 获取通过重定向传入的输入文件的名称?
- C++ 如何打开带有另一个目录的输入文件?
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 如何从输入文件 c++ 创建随机名称生成器
- 从输入文件修剪空格
- 链接器输入文件未使用,因为链接未完成,我无法获得.o
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 我遇到使用 c++ 逐行读取输入文件的问题
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 使用 getline 从输入文件打印时如何忽略空白行
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 数组大小由输入文件中的行数定义
- 从C++输入文件读取
- 在课堂上创建了一个朋友,给出"无输入文件"错误
- 通过显示一行星号来创建条形图,该条形图将读取数据的输入文件
- 利用输入文件中的数据(字符串和int)并在循环中利用
- C++ 仅读取输入文件中的部分(不是全部)数据以添加到记录中
- 如何从输入文件中读取字符并将其存储到向量中?