| | |
Student, help with fstream
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
I can never get this to work correctly for me, and I'm looking to you guys for help now. This program is going to eventually calculate the betweenness centrality for nodes on a graph, but I'm trying to get the file I/O to work properly. Any ideas? Code in tags and attached. (The fileIn.cpp file is normally named fileIn.cxx, and not specifically included in the build, as that is taken care of within the headers)
Also: I have tried to choose the file path manually, and that didn't work either. I'm coding on Vista x64, though I don't see why that should be an issue.
Also: I have tried to choose the file path manually, and that didn't work either. I'm coding on Vista x64, though I don't see why that should be an issue.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <vector> #include "fileIn.h" using namespace std; int main() { int vertices; vector<int> matrix; string fileName=getFileName(); getData(vertices,matrix,fileName); cout << vertices; for(int i=0;i<(vertices*vertices);i++) cout << matrix[i] << endl; return 0; } //main()
C++ Syntax (Toggle Plain Text)
#ifndef fileIn #define fileIn #include <iostream> #include <fstream> #include <string> #include <vector> #include "fileIn.cxx" using namespace std; string getFileName(); void getData(int &vertices, vector<int> &matrix, const string& fileName); #endif //fileIn
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <vector> #include "fileIn.h" using namespace std; string getFileName() { string fileName; string directory; size_t found; string currentFile=__FILE__; cout << "What is the name of the file to be analyzed? " << endl; getline(cin, fileName); found=currentFile.find_last_of("\\"); directory=currentFile.substr(0,found); directory+="\\data\\"; directory+=fileName; return directory; } void getData(int &vertices, vector<int> &matrix, const string& fileName) { int temp; ifstream openFile; openFile.open(fileName.c_str()); if(openFile.is_open()) { openFile>>vertices; while(!openFile.eof()) { openFile>>temp; matrix.push_back(temp); } openFile.close(); } else cout << "Unable to read file." << endl; }
Last edited by Cy137; Oct 18th, 2009 at 7:59 pm.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
0
#2 Oct 18th, 2009
Be more descriptive of the problem and you are more likely to get an answer. Not everyone is going to download, compile, run, analyze and then post a response.
After quickly reading through posted code in getData() i would change this: to this: to avoid off by one error that will surely pop up sooner or later because eof() won't equate to false until you try to read past EOF.
After quickly reading through posted code in getData() i would change this:
C++ Syntax (Toggle Plain Text)
while(!openFile.eof()) { openFile >> temp;
C++ Syntax (Toggle Plain Text)
while(openFile>>temp) {
Klatu Barada Nikto
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#3 Oct 18th, 2009
Okay, I made the change that you suggested above.
The issue is that no matter how I seem to input the string for the fileName, the openFile.isOpen() returns false. I have attempted to declare the full path manually, I have tried the method above, and I've tried leaving the file in the default directory, and inputting that file name. Again, no success there. I feel like I'm doing something fundamentally wrong with the ifstream syntax, but I have no idea what it is.
The issue is that no matter how I seem to input the string for the fileName, the openFile.isOpen() returns false. I have attempted to declare the full path manually, I have tried the method above, and I've tried leaving the file in the default directory, and inputting that file name. Again, no success there. I feel like I'm doing something fundamentally wrong with the ifstream syntax, but I have no idea what it is.
0
#4 Oct 18th, 2009
What compiler/IDE are you using? That would help us to tell you where the input file should be located. Are you running this from the IDE or directly in a command window?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
0
#6 Oct 18th, 2009
Hmm, bet you made the file in notepad and named it "test.txt", and Vista added the other .txt. Or VS might have done the same thing.
That's why I like to set my systems to display the file extensions.
That's why I like to set my systems to display the file extensions.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Multiple student database (C++)
- Help with Student Grades program (C++)
- Need some help with Student Grade program (C++)
- amigious problem with fstream (C++)
- Using Vectors and Arrays with fstream (C++)
Other Threads in the C++ Forum
- Previous Thread: Student needs help..please
- Next Thread: Need help with making a simple program! (newbie)
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






