By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

Why does fstream fail to open file, but ofstream opens ok?

P: 29
I am not sure what else to try in order to address this issue.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.    fstream mfile("./ftest.txt", fstream::in | fstream::out);
  9.    std::cout << "fstream fail: " << (mfile.fail() == 1 ? "true" : "false") << std::endl;
  10.    mfile.close();
  11.  
  12.    ifstream ifile("./itest.txt");
  13.    std::cout << "ifstream fail: " << (ifile.fail() == 1 ? "true" : "false") << std::endl;
  14.    ifile.close();
  15.  
  16.    ofstream ofile("./otest.txt");
  17.    std::cout << "ofstream fail: " << (ofile.fail() == 1 ? "true" : "false") << std::endl;
  18.    ofile << "1, 2, 3, test, test\n";
  19.    ofile.close();
  20.  
  21.    return(0);
  22. }
  23.  
And here is the output from the program:

Expand|Select|Wrap|Line Numbers
  1. [mquezada@otw14 ~/fstreamTest]$ g++ -o fstreamTest fstreamTest.cpp
  2. [mquezada@otw14 ~/fstreamTest]$ ./fstreamTest
  3. fstream fail: true
  4. ifstream fail: true
  5. ofstream fail: false
  6. [mquezada@otw14 ~/fstreamTest]$ ls -la
  7. total 52
  8. drwxrwxrwx  2 mquezada users  4096 Nov 22 11:15 .
  9. drwxr-xr-x 88 mquezada users 20480 Nov 22 10:39 ..
  10. -rwxr-xr-x  1 mquezada users  9484 Nov 22 11:15 fstreamTest
  11. -rw-r--r--  1 mquezada users   605 Nov 22 10:51 fstreamTest.cpp
  12. -rw-r--r--  1 mquezada users 12288 Nov 22 10:52 .fstreamTest.cpp.swp
  13. -rw-r--r--  1 mquezada users    20 Nov 22 11:15 otest.txt
  14. [mquezada@otw14 ~/fstreamTest]$ more otest.txt
  15. 1, 2, 3, test, test
  16. [mquezada@otw14 ~/fstreamTest]$
  17.  
As far as I can tell, all permissions are set correctly and there is no reason that I can see why both the fstream and the ifstream objects fail. By the way, I am running on a linux system with gcc version 4.1.2 20080704 (Red Hat 4.1.2-48).

-Marco
Nov 22 '10 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Your code works for me. I used the same file names as you did but removed the "./" since I am using windows.
Like this:

Expand|Select|Wrap|Line Numbers
  1. fstream mfile("ftest.txt", fstream::in | fstream::out);
So your difficulty may be in using a relative path but I can't say for sure as I don't use Linux.
Nov 22 '10 #2

P: 29
I tried it without the "./" also but I get the same result. I am wondering if this is an issue with the C++ library itself, at least my installation.
Nov 22 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I expect then that it's your permissions rather than the C++ library. I say this because Ihave not seen this reported before as a library problem.
Nov 23 '10 #4

P: 5
I think you should create this two file: itest.txt and ftest.txt first.

Is that right? Thank you!
Nov 24 '10 #5

P: 29
Actually, it turns out that fishlover's answer was not entirely wrong. I am not sure if other implementations of C++ handle this the same way but I eventually found out that fstream will fail if the file does not exist as pointed out by weaknessforcats. I expected ifstream to fail because in fact the itest.txt file did not exist. But I was not expecting fstream to fail in that case since it performs both input and output operations. I guess I wrongly expected its default behavior to be such that it creates the file if it is not there already. The solution in the end was to call fstream in the following way:

Expand|Select|Wrap|Line Numbers
  1. fstream mfile("./ftest.txt", fstream::in | fstream::out | fstream::trunc);
  2.  
The trunc flag will create the file if it does not exist. Of course this works for me but other applications would want to check for the file if they do not wish to overwrite its contents.

Thanks everyone for their time looking into this. It always helps to have another set of eyes.
Nov 24 '10 #6

P: 5
Hi,
It's really very strange.But I still believed that if you wanted to create an ifstream, the file must be created first.Its' not the trunc flag that create the file.
The effect of in|out|truc has the same functions as "w+" in C, it's effect is described as folowing: Truncate to 0 length, if existent, or create text file for update
For more information please refer to the following link http://stdcxx.apache.org/doc/stdlibug/30-3.html Table 33:Open modes and their C stdio counterparts.
Thank you!
Nov 25 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.