Reading input from file

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 3
Reputation: Aprentchacker is an unknown quantity at this point 
Solved Threads: 0
Aprentchacker Aprentchacker is offline Offline
Newbie Poster

Reading input from file

 
0
  #1
Aug 30th, 2009
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Reading input from file

 
0
  #2
Aug 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Reading input from file

 
0
  #3
Aug 30th, 2009
I did a mistake in my last post: the >> operator reads data until a end-of-line character OR a whitespace is found.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: Aprentchacker is an unknown quantity at this point 
Solved Threads: 0
Aprentchacker Aprentchacker is offline Offline
Newbie Poster

Re: Reading input from file

 
0
  #4
Aug 31st, 2009
Originally Posted by GDICommander View Post
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Reading input from file

 
0
  #5
Aug 31st, 2009
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.
  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. }
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: Aprentchacker is an unknown quantity at this point 
Solved Threads: 0
Aprentchacker Aprentchacker is offline Offline
Newbie Poster

Re: Reading input from file

 
0
  #6
Aug 31st, 2009
Originally Posted by dwks View Post
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.
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC