4

My code is crashing upon reading from a file (seen at the end of this post). I am declaring an ifstream object in main, passing it through a buildGraph function (which takes ifstream& as a parameter), and attempting to pass the first token into a string temp.

Relevant code from main:

#include <fstream>

int main() {

    ifstream infile1("data31.txt");

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

    GraphM G;
    G.buildGraph(infile1);
}

Relevant code from graphm.cpp:

#include <fstream>
#include <string>

void GraphM::buildGraph(ifstream& input)
{
    string temp;
    input >> temp;
}

There is a header file graphm.h that includes fstream as well. I talked with a couple of tutors who work at the college I'm at and they haven't been able to help at all, as they are as confused as I am. The function getline() throws a segmentation fault, as well, so that won't work. What am I doing wrong, here?

Also, the .txt I'm reading from:

5
Aurora and 85th
Green Lake Starbucks
Woodland Park Zoo
Troll under bridge
PCC
1 2 50
1 3 20
1 5 30
2 4 10
3 2 20
3 4 40
5 2 20
5 4 25
0 0  0
3
aaa
bbb
ccc
1 2 10
1 3 5
2 3 20
3 2 4
0 0 0
Muddy
  • 77
  • 2
  • 6

2 Answers2

0

(Comment included as an answer to allow code formatting)

To construct a SSCCE I included all the code in one file as follows:

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class GraphM {
public:
    void buildGraph(ifstream& input);
};

void GraphM::buildGraph(ifstream& input)
{
    string temp;
    input >> temp;
    cout << temp;
}


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

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

    GraphM G;
    G.buildGraph(infile1);
}

I then compiled and ran the code and obtained the expected output 5.

I then separated the code into the three files described in the question, compiled and ran them and again got the same result, 5. I'm using GNU C++ 4.7.3 in cygwin on Windows 7.

Could you tell us more about the compiler you are using and other details of your system?

Simon
  • 9,962
  • 1
  • 26
  • 43
  • This is more of a comment than an answer. – 0x499602D2 Apr 25 '14 at 03:51
  • Yeah, this is pretty much exactly the code I have. :| I'm using g++ compiler through my campus' linux machines. I'm on Windows 8 and my IDE is Visual Studio 2012, but that shouldn't make a difference regarding why this is crashing – Muddy Apr 25 '14 at 17:23
  • 1
    @user2844013: You should be able to compile the same code on VS2012 and on the Linux G++ compiler and get exactly the same result -- i.e. with the code above you shouldn't get a segmentation fault in either case. Do you get the same result (segmentation fault) with both compilers? If so, can you provide a SSCCE that produces that segmentation fault? Given that my code above does not produce the fault (on my computer and compiler - but how about yours?), there must be something in the code that you haven't shown us that is causing the problem. – Simon Apr 25 '14 at 19:50
0
    #include <fstream>
#include <iostream>
#include <string>

using namespace std;


void buildGraph(ifstream& input)
{
    string temp;

    for(int i=0; i<sizeof(input); i++)
    {
        input >> temp;
        cout<< temp;
    }
}


   int main()
   {

      ifstream infile1("test.txt");

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

      buildGraph(infile1);
   }

i think dumping a file into a string might be your problem... the code i posted gave me no problem, however you might want to try using char* to catch your file, and then break it to whatever you want.

orpgol
  • 13
  • 1
  • 4