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.