944,045 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 450
  • C++ RSS
Oct 18th, 2009
0

Student, help with fstream

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

C++ Syntax (Toggle Plain Text)
  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()
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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. }
Attached Files
File Type: cpp main.cpp (373 Bytes, 3 views)
File Type: h fileIn.h (277 Bytes, 9 views)
File Type: cpp fileIn.cpp (850 Bytes, 3 views)
Last edited by Cy137; Oct 18th, 2009 at 7:59 pm.
Similar Threads
Reputation Points: 15
Solved Threads: 0
Newbie Poster
Cy137 is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student, help with fstream
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:
C++ Syntax (Toggle Plain Text)
  1. while(!openFile.eof())
  2. {
  3. openFile >> temp;
to this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 18th, 2009
0
Re: Student, help with fstream
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.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
Cy137 is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student, help with fstream
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?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 18th, 2009
1
Re: Student, help with fstream
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.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
Cy137 is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student, help with fstream
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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

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: Student needs help..please
Next Thread in C++ Forum Timeline: Need help with making a simple program! (newbie)





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


Follow us on Twitter


© 2011 DaniWeb® LLC