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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • meLlamanJefe
    New Member
    • Mar 2008
    • 29

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

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

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       fstream mfile("./ftest.txt", fstream::in | fstream::out);
       std::cout << "fstream fail: " << (mfile.fail() == 1 ? "true" : "false") << std::endl;
       mfile.close();
    
       ifstream ifile("./itest.txt");
       std::cout << "ifstream fail: " << (ifile.fail() == 1 ? "true" : "false") << std::endl;
       ifile.close();
    
       ofstream ofile("./otest.txt");
       std::cout << "ofstream fail: " << (ofile.fail() == 1 ? "true" : "false") << std::endl;
       ofile << "1, 2, 3, test, test\n";
       ofile.close();
    
       return(0);
    }
    And here is the output from the program:

    Code:
    [mquezada@otw14 ~/fstreamTest]$ g++ -o fstreamTest fstreamTest.cpp
    [mquezada@otw14 ~/fstreamTest]$ ./fstreamTest
    fstream fail: true
    ifstream fail: true
    ofstream fail: false
    [mquezada@otw14 ~/fstreamTest]$ ls -la
    total 52
    drwxrwxrwx  2 mquezada users  4096 Nov 22 11:15 .
    drwxr-xr-x 88 mquezada users 20480 Nov 22 10:39 ..
    -rwxr-xr-x  1 mquezada users  9484 Nov 22 11:15 fstreamTest
    -rw-r--r--  1 mquezada users   605 Nov 22 10:51 fstreamTest.cpp
    -rw-r--r--  1 mquezada users 12288 Nov 22 10:52 .fstreamTest.cpp.swp
    -rw-r--r--  1 mquezada users    20 Nov 22 11:15 otest.txt
    [mquezada@otw14 ~/fstreamTest]$ more otest.txt
    1, 2, 3, test, test
    [mquezada@otw14 ~/fstreamTest]$
    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
    Last edited by meLlamanJefe; Nov 22 '10, 04:21 PM. Reason: Added gcc version info.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your code works for me. I used the same file names as you did but removed the "./" since I am using windows.
    Like this:

    Code:
    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.

    Comment

    • meLlamanJefe
      New Member
      • Mar 2008
      • 29

      #3
      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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        • fishlover
          New Member
          • Nov 2010
          • 5

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

          Is that right? Thank you!
          Last edited by fishlover; Nov 24 '10, 08:44 AM. Reason: I am sorry, but I have give a wrong answer.

          Comment

          • meLlamanJefe
            New Member
            • Mar 2008
            • 29

            #6
            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:

            Code:
            fstream mfile("./ftest.txt", fstream::in | fstream::out | [B]fstream::trunc[/B]);
            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.

            Comment

            • fishlover
              New Member
              • Nov 2010
              • 5

              #7
              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!
              Last edited by fishlover; Nov 25 '10, 01:55 PM. Reason: I have give a wrong link,sorry

              Comment

              Working...