943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 798
  • C++ RSS
Aug 30th, 2009
0

Reading input from file

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. template<class vType, int size>
  2. void graphType<vType, size>::createGraph()
  3. {
  4. ifstream infile;
  5. char fileName[50];
  6.  
  7. vType vertex;
  8. vType adjacentVertex;
  9.  
  10. if(gSize != 0) //if the graph is not empty, make it empty
  11. clearGraph();
  12.  
  13. cout<<"Enter the input file name: ";
  14. cin>>fileName;
  15. cout<<endl;
  16.  
  17. infile.open(fileName);
  18.  
  19. if(!infile)
  20. {
  21. cerr<<"Cannot open the input file."<<endl;
  22. return;
  23. }
  24.  
  25. infile>>gSize; //get the number of vertices
  26.  
  27. for(int index = 0; index < gSize; index++)
  28. {
  29. infile>>vertex;
  30. infile>>adjacentVertex;
  31.  
  32. while(adjacentVertex != -999)
  33. {
  34. graph[vertex].insertLast(adjacentVertex);
  35. infile>>adjacentVertex;
  36. }//end while
  37. }//end for
  38.  
  39. infile.close();
  40. }//end createGraph
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aprentchacker is offline Offline
7 posts
since Aug 2009
Aug 30th, 2009
0

Re: Reading input from file

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?
Last edited by GDICommander; Aug 30th, 2009 at 8:09 pm.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Aug 30th, 2009
0

Re: Reading input from file

I did a mistake in my last post: the >> operator reads data until a end-of-line character OR a whitespace is found.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Aug 31st, 2009
0

Re: Reading input from file

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:

C++ Syntax (Toggle Plain Text)
  1. 5
  2. 0 1 4 3 -99
  3. 1 2 -99
  4. 2 -99
  5. 3 4 1 -99
  6. 4 2 1 -99
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aprentchacker is offline Offline
7 posts
since Aug 2009
Aug 31st, 2009
0

Re: Reading input from file

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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. std::string line;
  6. for(int row = 0; std::getline(infile, line); row ++) {
  7. std::istringstream stream(line); // create a stringstream with the line from the file
  8. int number;
  9. while(stream >> number) {
  10. graph[row].insertLast(number);
  11. }
  12. }
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 31st, 2009
0

Re: Reading input from file

Click to Expand / Collapse  Quote originally posted by dwks ...
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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. std::string line;
  6. for(int row = 0; std::getline(infile, line); row ++) {
  7. std::istringstream stream(line); // create a stringstream with the line from the file
  8. int number;
  9. while(stream >> number) {
  10. graph[row].insertLast(number);
  11. }
  12. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aprentchacker is offline Offline
7 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: linking files and templates
Next Thread in C++ Forum Timeline: Sorting alphabetically a linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC