无法读取文件的所有行,即使之前使用 C++ 读取了具有相同结构的行

Can't read all the lines of a file even when a line with the same structure has been read before with C++

本文关键字:读取 C++ 结构 文件      更新时间:2023-10-16

我正在尝试阅读一些文本文件,并将每个单词放在不同的变量中。由于某些原因,我找不到,文件没有被读取到文件的末尾,它突然停止而不是读取所有行。文件停止读取的行具有先前读取没有问题的前一行的结构,因此我在没有看到问题在哪里的情况下感到困惑。

我试图阅读测试代码的文件,可以看到:在这里。我能读懂的只有以下几行:

    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 */
    }
}