| | |
problem opening files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
Hey guys I am using code that does the following:
The problem is that if a file name is entered that does not exist for example c:\\fred.txt the error message will not appear and the txt file fread will be created, but if I enter "c:\\fred.txt" the code seems to work, but I have created similar code before using cin.getline and the quotes were not needed. Any ideas what I have done wrong?
Thanks.
C++ Syntax (Toggle Plain Text)
cin.ignore(); cin.getline(file, 250); ifstream test; test.open (file); if (test.fail()) { cout<<"error"<<endl; exit(1); } ...
The problem is that if a file name is entered that does not exist for example c:\\fred.txt the error message will not appear and the txt file fread will be created, but if I enter "c:\\fred.txt" the code seems to work, but I have created similar code before using cin.getline and the quotes were not needed. Any ideas what I have done wrong?
Thanks.
when you enter the filename from the keyboard do not use double backslash -- double slashes are only needed for string literals in your program. So you should type c:\fred.txt , only one backslash. Reason: the compiler does not interpret anything you enter from the keyboard, it only interprets string literals.
•
•
Join Date: Jul 2005
Posts: 91
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
then the problem is elsewhere. try test.is_open() instead of if test.fail(). Use your compiler's debugger and look at what exactly is in the file buffer, or use cout to display it.
![]() |
Similar Threads
- problem with opening 2 or more files in c++ (C++)
- problem opening CSV file (C++)
- Several problem with video files (Windows Software)
- Big problem please help (Viruses, Spyware and other Nasties)
- Trouble opening certain files... (Windows 95 / 98 / Me)
Other Threads in the C++ Forum
- Previous Thread: Best solution?
- Next Thread: Getting all data from an input and output file
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






