无法读取文件的所有行,即使之前使用 C++ 读取了具有相同结构的行
Can't read all the lines of a file even when a line with the same structure has been read before with C++
我正在尝试阅读一些文本文件,并将每个单词放在不同的变量中。由于某些原因,我找不到,文件没有被读取到文件的末尾,它突然停止而不是读取所有行。文件停止读取的行具有先前读取没有问题的前一行的结构,因此我在没有看到问题在哪里的情况下感到困惑。
我试图阅读测试代码的文件,可以看到:在这里。我能读懂的只有以下几行:
1 1gat inpt 1 0 >sa1
2 2gat inpt 1 0 >sa1
3 3gat inpt 2 0 >sa0 >sa1
8 8fan from 3gat >sa1
9 9fan from 3gat >sa1
6 6gat inpt 1 0 >sa1
7 7gat inpt 1 0 >sa1
10 10gat nand 1 2 >sa1
1 8
11 11gat nand 2 2 >sa0 >sa1
我编写的读取它的代码如下:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string line, netlist;
string name, type, fsa0, fsa1, temp;
int address, fanout, fout, fin, *FIN, max_node = 0;
cout << "Wich Netlist you want to use?" << endl;
cin >> netlist;
ifstream file(netlist.c_str());
if (file.is_open()) {
while (getline(file, line)) {
int temfalha = 0;
long pfile; // conferir posição em file
if ( line[0] != '*' ) {
name = type = temp = fsa0 = fsa1 = "@";
address = fanout = fout = fin = 0;
file >> address >> name >> type;
// checar se existe falha
pfile = file.tellg(); //coloca em pfile a posição em que o ponteiro está.
char a;
for (int i = 0; i < 20; i++) {
file >> a;
if (a == '>') {
temfalha += 1;
//break;
}
}
file.seekg(pfile); //coloca o ponteiro na posição pfile
if ( type == "from") {
file >> fanout >> temp;
if (temfalha>0) {
file >> fsa0;
if ( temfalha == 1) {
if (fsa0 == ">sa0" ) { fsa1 = "@"; }
else { fsa1 = fsa0; fsa0 = "@"; }
if ( temfalha == 2) { file >> fsa1; }
}
}
} else if (type == "inpt") {
file >> fout >> fin;
if (temfalha>0) {
file >> fsa0;
if ( temfalha == 1) {
if (fsa0 == ">sa0" ) { fsa1 = "@"; }
else { fsa1 = fsa0; fsa0 = "@"; }
if ( temfalha == 2) { file >> fsa1; }
}
}
} else {
file >> fout >> fin;
if (temfalha>0) {
file >> fsa0;
if ( temfalha == 1) {
if (fsa0 == ">sa0" ) { fsa1 = "@"; }
else { fsa1 = fsa0; fsa0 = "@"; }
if ( temfalha == 2) { file >> fsa1; }
}
}
FIN = new int[fin];
for (int i= 0; i < fin; i++) file >> FIN[i];
}
if ( address > max_node )
max_node = address;
cout << address <<" "<< name <<" "<< type <<" "<< fanout <<" "<< temp <<" "<< fout <<" "<< fin <<" "<< fsa0 <<" "<< fsa1 <<" "<< endl;
}
}
cout << max_node << endl;
} else { cout << "File not found" << endl; }
file.close();
return 0;
}
p。:代码中的"@"是我选择用来填充文件空白的字符。
你们能看出问题在哪里吗?我是c++新手,所以,谢谢你的帮助。
然后执行getline
,将一行读入line
变量并移动文件指针。因此,您稍后执行file >> address >> name >> type;
,您正在读取文件的第二行。getline
读取的线路上的数据丢失。你可以做的是,使用stringstream
从string:
while (std::getline(file, line))
{
if (line[0] != '*')
{
std::istringstream iss(line);
iss >> address >> name >> type;
/* work with iss here */
}
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 一种在C++中读取TXT配置文件的简单方法