1

I am having trouble with the >> operator of iostream when reading a .txt file. The error is happening at the bottom but here is some code to give some context.

MAIN - I cannot change this file for assignment purposes.

#include <iostream>
#include <fstream>
#include "graphm.h"
using namespace std;

int main() {
    
    ifstream infile1("data31.txt");

    if (!infile1) {
        cout << "File could not be opened." << endl;
        return 1;
    }

    GraphM G;
    G.buildGraph(infile1);
    ...
}

Below is the method where the error is, Build Graph in graphm.cpp.

#include <iomanip>
#include <iostream>
#include <fstream>


using namespace std;

GraphM::GraphM()
{
    size = 0;
    for (int i = 1; i <= MAXNODES; i++)
    {
        for(int j = 1; j <= MAXNODES; j++)
        {
            C[i][j] = INT_MAX;
            T[i][j].dist = INT_MAX;
            T[i][j].path = 0;
            T[i][j].visited = false;
        }
    }
}

void GraphM::buildGraph(ifstream& infile)
{
    int s;
    infile >> s;
    size = s;
    //I have also tried just infile >> size; and that doesn't work either.
    ...
}

I get an error that says "EXC_BAD_ACCESS (code=1, address=0x7fffffe7)" on the infile >> s; line.

I tried running on a linux server and it compiled with no error but when running at this point it said "Segmentation Fault".

I am using CLion on a mac. I don't believe it has to do with my code but maybe the IDE... however the error also happened in linux.

The file reads in and does not hit the "Cannot find file" point, but maybe it doesnt read in correctly? In CLion, data31.txt it is inside the debug folder as well as in the same directory as the main. I also tried the line below just to give it a shot for the first line in main and it lead to the same outcome.

ifstream infile1("/Users/stlp/Documents/CSS343/ass3/assignment3_supportingFiles/data31.txt");

EDIT - showed the entire constructor. Also to be clear, the code runs until the last line listed.

Jon Walzer
  • 65
  • 1
  • 6
  • Something seriously wrong here (assuming you've described your code correctly). The code above cannot produce the result you describe in a working C++ installation. But of course maybe you missed out some important detail. If so then it's hard to help until we know what that detail is. – john Nov 07 '20 at 06:34
  • Just because this is where the program crashes or reports an error doesn't mean that's where the problem is. C++ does not work this way. The problem can be anywhere in your code, but after the bug occurs the program keeps running for a little bit before it finally crashes here. This is why stackoverflow.com's [help] requires you to show a [mre] that anyone can cut/paste ***exactly as shown***, then compile, run, and reproduce your problem. See [ask] for more information. Until you do that, it is unlikely that anyone will be able to figure out your problem. – Sam Varshavchik Nov 07 '20 at 06:34
  • If I had to guess I would say the real error is in your `GraphM` constructor. Clearly the code shown above is not the complete code. – john Nov 07 '20 at 06:38
  • I tried to make the code more concise by not including the full constructor. I will update it. – Jon Walzer Nov 07 '20 at 06:52

0 Answers0