Student, help with fstream

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 4
Reputation: Cy137 is an unknown quantity at this point 
Solved Threads: 0
Cy137 Cy137 is offline Offline
Newbie Poster

Student, help with fstream

 
0
  #1
Oct 18th, 2009
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.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. #include "fileIn.h"
  7.  
  8.  
  9. using namespace std;
  10.  
  11.  
  12. int main()
  13. {
  14. int vertices;
  15. vector<int> matrix;
  16. string fileName=getFileName();
  17. getData(vertices,matrix,fileName);
  18.  
  19. cout << vertices;
  20. for(int i=0;i<(vertices*vertices);i++)
  21. cout << matrix[i] << endl;
  22. return 0;
  23. } //main()
  1. #ifndef fileIn
  2. #define fileIn
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7. #include <vector>
  8. #include "fileIn.cxx"
  9.  
  10. using namespace std;
  11.  
  12. string getFileName();
  13. void getData(int &vertices, vector<int> &matrix, const string& fileName);
  14.  
  15. #endif //fileIn
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. #include "fileIn.h"
  7.  
  8. using namespace std;
  9.  
  10. string getFileName()
  11. {
  12. string fileName;
  13. string directory;
  14. size_t found;
  15. string currentFile=__FILE__;
  16.  
  17. cout << "What is the name of the file to be analyzed? " << endl;
  18. getline(cin, fileName);
  19.  
  20. found=currentFile.find_last_of("\\");
  21. directory=currentFile.substr(0,found);
  22. directory+="\\data\\";
  23. directory+=fileName;
  24.  
  25. return directory;
  26. }
  27.  
  28. void getData(int &vertices, vector<int> &matrix, const string& fileName)
  29. {
  30. int temp;
  31. ifstream openFile;
  32. openFile.open(fileName.c_str());
  33. if(openFile.is_open())
  34. {
  35. openFile>>vertices;
  36. while(!openFile.eof())
  37. {
  38. openFile>>temp;
  39. matrix.push_back(temp);
  40. }
  41. openFile.close();
  42. }
  43. else
  44. cout << "Unable to read file." << endl;
  45. }
Last edited by Cy137; Oct 18th, 2009 at 7:59 pm.
Attached Files
File Type: cpp main.cpp (373 Bytes, 0 views)
File Type: h fileIn.h (277 Bytes, 0 views)
File Type: cpp fileIn.cpp (850 Bytes, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso
 
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:
  1. while(!openFile.eof())
  2. {
  3. openFile >> temp;
to this:
  1. while(openFile>>temp)
  2. {
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: Cy137 is an unknown quantity at this point 
Solved Threads: 0
Cy137 Cy137 is offline Offline
Newbie Poster
 
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: Cy137 is an unknown quantity at this point 
Solved Threads: 0
Cy137 Cy137 is offline Offline
Newbie Poster
 
1
  #5
Oct 18th, 2009
Hmph. I apologize for wasting everyones' time here. Turns out that the file somehow got misnamed to "test.txt.txt" and that explains all the issues that went with it. I appreciate the help though, and if a mod wants to lock this/ect, that would be great.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
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.
"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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC