| | |
Reading input from file
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
Here is the function code for refence:
C++ Syntax (Toggle Plain Text)
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'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.
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
I did a mistake in my last post: the >> operator reads data until a end-of-line character OR a whitespace is found.
C++ Syntax (Toggle Plain Text)
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.
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)
#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); } }
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
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
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
#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.
![]() |
Similar Threads
- reading Input from File (Java)
- Need some help with reading from input file [newbie] (Python)
- Reading a input file (C)
- Reading an input file as a class memeber function (C++)
- I need help on STRUCTURES and on input file (C)
- How to Read an input file (C++)
Other Threads in the C++ Forum
- Previous Thread: linking files and templates
- Next Thread: Sorting alphabetically a linked list
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





