Finding end of file while reading from it

后端 未结 5 1034
夕颜
夕颜 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:18

    Try moving first two reads in the while condition:

    // assuming Gtable has at least size of 1
    
    while( fin>>Gtable[slot].Name && fin>>Gtable[slot].Out ) {
        cout<>X;
            cout<

    Edit: As per James Kanze's comment, you're taking an adress past the end of Gtable array, which is what causes segfault. You could pass the size of Gtable as argument to your fillTable() function (f.ex. void fillTable(int table_size)) and check slot is in bounds before each read.

提交回复
热议问题