Finding end of file while reading from it

后端 未结 5 1036
夕颜
夕颜 2021-01-16 06:09
void graph::fillTable()
{
  ifstream fin;
  char X;
  int slot=0;

  fin.open(\"data.txt\");

  while(fin.good()){

  fin>>Gtable[slot].Name;
  fin>>Gtab         


        
5条回答
  •  无人共我
    2021-01-16 06:25

    Slightly slower but cleaner approach:

    void graph::fillTable()
    {
      ifstream fin("data.txt");
      char X;
      int slot=0;
    
      std::string line;
    
      while(std::getline(fin, line))
      {
        if (line.empty()) // skip empty lines
          continue;
    
        std::istringstream sin(line);
        if (sin >> Gtable[slot].Name >> Gtable[slot].Out && Gtable[slot].Out > 0)
        {
          std::cout << Gtable[slot].Name << std::endl;
          for(int i = 0; i < Gtable[slot].Out; ++i)
          {
            if (sin >> X)
            {
              std::cout << X << std::endl;
              Gtable[slot].AdjacentOnes.addFront(X);
            }
          }
          slot++;
        }
      }
    }
    

    If you still have issues, it's not with file reading...

提交回复
热议问题