Unable to open input file with fstream

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

Join Date: Apr 2008
Posts: 10
Reputation: harkw002_UniSA is an unknown quantity at this point 
Solved Threads: 0
harkw002_UniSA's Avatar
harkw002_UniSA harkw002_UniSA is offline Offline
Newbie Poster

Unable to open input file with fstream

 
0
  #1
Apr 6th, 2008
Unable to open file with fstream. it always seems to jump to the else statement and gives me the error message "Error: can't open input file ". I have included in the header file

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>

and in the class file i have the following code and yes i do have a txt file called studentFileName.txt in the same directory.

  1. void StudentRecord::addFile(const string& studentFileName){
  2. ifstream infile;
  3. infile.open ("studentFileName.txt");
  4. if (infile.is_open()){
  5. string line, word, StudentName, ProgramCode;
  6. while(getline(cin, line)){
  7. istringstream stream(line);
  8. while (stream >> word) {
  9. int i = 0;
  10. if (i % 2 == 0) {
  11. StudentName = word;
  12. }
  13. else {
  14. ProgramCode = word;
  15. }
  16. StudentRecord::add(StudentName, ProgramCode);
  17. }
  18. }
  19. infile.close();
  20. }
  21. else {
  22. cout << "Error: can't open input file " << endl;
  23. }
  24. }

Any solutions would be greatly appreciated as i am completely stumped.

Thankyou
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,577
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Unable to open input file with fstream

 
0
  #2
Apr 6th, 2008
The file is not in the current directory. If you are using MS-Windows then use Explorer to verify the location of the file and add the full path to the filename.
infile.open ("c:\\MyFolder\\studentFileName.txt");
Last edited by Ancient Dragon; Apr 6th, 2008 at 12:42 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: harkw002_UniSA is an unknown quantity at this point 
Solved Threads: 0
harkw002_UniSA's Avatar
harkw002_UniSA harkw002_UniSA is offline Offline
Newbie Poster

Re: Unable to open input file with fstream

 
0
  #3
Apr 6th, 2008
Someone on another forum solved this one for me

Maybe try this:

  1. infile.open(file.c_str());

substitute file for your file.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC