Please help me undersatnd why this code does no work, even when it compiled without problems. After I have input the filename the application just hangs up and aborts thereafter. My input file contains the graph' adjacency matrix and the application is supposed to pull this data from the file to create a graph object. Any explanations about whats wrong will be greatly appreciated.

Here is the function code for refence:

template<class vType, int size>
void graphType<vType, size>::createGraph()
{
     ifstream infile;
     char fileName[50];

     vType vertex;
     vType adjacentVertex;

     if(gSize != 0)  //if the graph is not empty, make it empty
        clearGraph();

     cout<<"Enter the input file name: ";
     cin>>fileName;
     cout<<endl;

     infile.open(fileName);

     if(!infile)
     {
        cerr<<"Cannot open the input file."<<endl;
        return;
     }

     infile>>gSize;  //get the number of vertices

     for(int index = 0; index < gSize; index++)
     {
         infile>>vertex;
         infile>>adjacentVertex;

         while(adjacentVertex != -999)
         {
             graph[vertex].insertLast(adjacentVertex);
             infile>>adjacentVertex;
         }//end while
     }//end for

     infile.close();
}//end createGraph

Are you sure that the file denoted with the name you provided exists?

I'm pretty sure that the answer is yes, but do you really have gSize vertices in your file? Do you really have a line at each end of a vertex definition in your file that is -999? Do you have a operator!= that takes a int has a parameter?

Remember that the >> operator reads data until a end-of-line character is found (this operator reads a line in a file).

If it's possible, can you post the content of the input file?

I did a mistake in my last post: the >> operator reads data until a end-of-line character OR a whitespace is found.

I did a mistake in my last post: the >> operator reads data until a end-of-line character OR a whitespace is found.

Thanks GDIComm for taking you time to look at this. here is my input file. I dont know whether i was supposed to name it differently, but its a text file created with notepad. First line is the number of vertices, and then subsequent lines denote each vertex and its adjacent vertices terminated by -99. Following is the format of the input file:

5
0 1 4 3 -99
1 2 -99
2 -99
3 4 1 -99
4 2 1 -99

You use -99 in your input file to indicate end of line, but you seem to expect -999 in your program . . . .

Just a thought: why don't you read in one line at a time, and treat that as the row of a matrix. That way you don't need any terminating numbers at all, which are really more trouble than they're worth.

#include <iostream>
#include <sstream>
#include <string>

std::string line;
for(int row = 0; std::getline(infile, line); row ++) {
    std::istringstream stream(line);  // create a stringstream with the line from the file
    int number;
    while(stream >> number) {
        graph[row].insertLast(number);
    }
}

You use -99 in your input file to indicate end of line, but you seem to expect -999 in your program . . . .

Just a thought: why don't you read in one line at a time, and treat that as the row of a matrix. That way you don't need any terminating numbers at all, which are really more trouble than they're worth.

#include <iostream>
#include <sstream>
#include <string>

std::string line;
for(int row = 0; std::getline(infile, line); row ++) {
    std::istringstream stream(line);  // create a stringstream with the line from the file
    int number;
    while(stream >> number) {
        graph[row].insertLast(number);
    }
}

DWKS and GDICommander,

I cant thank you enough, thanks sooooo much you guys, I feel stupid as all my troubles were caused by my input file. I am now able to test some of the member functions of my graphType. Also I feel this has exposed my shaky understanding of file I/O library. I am still embedded on my txtbk trying to iron this once and for all.
Any ways thanks so much guys. @ DWKS I will also tinker with your suggested code/method; looks easier.

This question has already been answered. Start a new discussion instead.