I'm trying to read a file and load it into an array in c ++ like this:
int main(int argc, char **argv){
if (argc != 2){
cerr << "Sample call <mat.cc> <filename>" << endl;
}
string filename(argv[1]);
MAT G = readGraph(filename);
My readGraph function does this:
using namespace std;
using Mat = vector<vector<int>>;
vector<vector<int>> readGraph(string fileName){
Mat g;
ifstream infile(fileName);
string line;
while(getline(infile, line)){
istringstream iss(line);
if (line[0] == 'p'){
string s1, s2;
int nodes;
iss >> s1 >> s2 >> nodes;
g.resize(nodes);
for(int i = 0; i < nodes ; i++){
g[i].resize(nodes);
}
cout << "Graph with " << nodes << " nodes" << endl;
} else if (line[0] == 'e'){
char e;
int s, t, w;
iss >> e >> s >> t;
g[ s - 1][t - 1] = 1;
}
}
return g;
}
When I compile my program g ++ -o mat mat.cc rome99.txt I get the following error:
/usr/bin/ld:rome99.txt: file format not recognized; treating as linker script
/usr/bin/ld:rome99.txt:1: syntax error
collect2: error: ld returned 1 exit status
I need help I have tried all the forms and does not recognize the file, the file contains something like this:
p sp 3353 8870
a 2958 2959 535
a 1494 1573 77
a 1590 1603 184
a 2634 2635 179
a 1984 1985 22
a 1453 1454 33
a 1453 1450 48
a 697 698 50
a 1447 1453 23
a 2036 2034 71
a 564 566 191
The line p indicates nodes and arcs ...