| | |
Unable to open input file with fstream
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
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.
Any solutions would be greatly appreciated as i am completely stumped.
Thankyou
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #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.
C++ Syntax (Toggle Plain Text)
void StudentRecord::addFile(const string& studentFileName){ ifstream infile; infile.open ("studentFileName.txt"); if (infile.is_open()){ string line, word, StudentName, ProgramCode; while(getline(cin, line)){ istringstream stream(line); while (stream >> word) { int i = 0; if (i % 2 == 0) { StudentName = word; } else { ProgramCode = word; } StudentRecord::add(StudentName, ProgramCode); } } infile.close(); } else { cout << "Error: can't open input file " << endl; } }
Any solutions would be greatly appreciated as i am completely stumped.
Thankyou
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.
Someone on another forum solved this one for me
Maybe try this:
substitute file for your file.
Maybe try this:
C++ Syntax (Toggle Plain Text)
infile.open(file.c_str());
substitute file for your file.
![]() |
Similar Threads
- from input file to array (C++)
- A lovely mixture of random numbers and arrays (C++)
- Array trouble (C++)
- Need help using a string input file??? (C++)
- only a couple of hours to go... (C++)
- Need help with a function (C++)
- Homework Help/Feedback (C++)
Other Threads in the C++ Forum
- Previous Thread: Share global variables between classes
- Next Thread: Few simple questions about C++
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






