0

Currently i'm trying to read numbers for a graph. I'm using an array of vectors because i know the amount of vertices i'm using. So the text file looks like this:

1 2 3 4
2 1
3 1
4 1

So my print functions works perfectly and i'm trying to get it to print like this:

Adjacency list of vertex 0
 head 

 Adjacency list of vertex 1
 head -> 2-> 3-> 4

 Adjacency list of vertex 2
 head -> 1

 Adjacency list of vertex 3
 head -> 1

 Adjacency list of vertex 4
 head -> 1

But it does not stop here. If i add a printf("%d", number); at the end, it stops before any issues with seg fault ( i understand that it is C but i used it by mistake and noticed it stopped right before doing anything wrong.

Here is my main:

int main()
{
    int V = 5;
    vector<int> adj[V];
    FILE *file = fopen("bfs1.txt", "r");
    if(file == 0){
      cout << "ERROR: file does not exist" << endl;
      return -1;
    }
    else {
      int x;
      fscanf(file, "%d", &x);
      int indexChange = 1;
      while(!feof(file)) {
        int c = getc(file);
        int number = c - '0';
        //to ignore spaces
        if (number == -16 ){
          continue;
        }
        //NewLine so increase vertex index
        if (number == -38){
          indexChange = indexChange + 1;
          continue;
        }
        //End of file
        if (number == -49){
          printGraph(adj,V);
          break;
        }
        addEdge(adj, indexChange, number);
        printGraph(adj,V);
        //printf("%d\n", number);
       }
     }
    return 0;
}

With this changes it worked for me but i don't know why its working.

if (number == -38){
          indexChange = indexChange + 1;
          break;
        }
STOPIMACODER
  • 822
  • 2
  • 7
  • 19
  • Totally unrelated: change `int V = 5;` to `const int V = 5;` or your code is illegal. `V` shouldn't change anyway, so making the sucker `const` also helps you catch potential bugs. – user4581301 May 04 '19 at 02:58
  • 1
    You may have run up against [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) but I need to keep reading to be sure. – user4581301 May 04 '19 at 02:59
  • Note: This is A LOT easier to write using C++ iostreams. [See option2 in this answer](https://stackoverflow.com/a/7868998/4581301). – user4581301 May 04 '19 at 03:03
  • OK. This cryptic-looking line of doom, `if (number == -49){` should prevent the usual `while (!eof)` problem. Consider placing `if (c == EOF) { printGraph(adj,V); break; }` after `int c = getc(file);` to get code that's a bit easier to read. Also update your question with a [mcve]. – user4581301 May 04 '19 at 03:13
  • @user4581301 as in provide the print function? – STOPIMACODER May 04 '19 at 03:16
  • @user4581301 Also thanks for the `c == EOF` ! totally skipped my mind – STOPIMACODER May 04 '19 at 03:18
  • You could do that, or you could Follow the advice in the [mcve] link and remove from your program everything that is not bug and post code that is nothing but the bug. Odds are there won't be any need for that because once you chop the program down until it's nothing but bug, it's really hard to not have already figured out the bug. – user4581301 May 04 '19 at 03:20
  • Side note: You'll likely never see one, but not all computers use ASCII. That means all of the `if (number == X )` statements could be dead wrong. You aren't coding on an old IBM mainframe, are you? – user4581301 May 04 '19 at 03:23
  • Is `if (number == -49)` not checking `c == EOF`? (in a backwards way?) You should check `c` not `number` (e.g. the result of `c = '0'`) If I understand you are checking `c == ' '` with `if (number == -16 )` and `c == '\n'` with `if (number == -38)` -- just in an awkwardly unreadable way. – David C. Rankin May 04 '19 at 03:23
  • @DavidC.Rankin it is, most likely, I suggested the `c == EOF` because it's a lot easier to understand. – user4581301 May 04 '19 at 03:25
  • I like your `"cryptic-looking line of doom"` comment `:)` – David C. Rankin May 04 '19 at 03:26
  • I think i figured it out but idk why it works. Edited to provide changes – STOPIMACODER May 04 '19 at 03:27
  • That's not crashing, but I don't think it's working. That should read one line and then exit the loop and subsequently the program. – user4581301 May 04 '19 at 03:30
  • Actually its working. I used the print function before the while loop and it printed empty but afterwards it printed correctly – STOPIMACODER May 04 '19 at 03:32
  • Weird. I guess I'd also have to see the expected behaviour – user4581301 May 04 '19 at 03:32

0 Answers0