473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.

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
6 14172
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
I think you should create this two file: itest.txt and ftest.txt first.

Is that right? Thank you!
Nov 24 '10 #5
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
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

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

Similar topics

14
by: Dave Murray | last post by:
New to Python question, why does this fail? Thanks, Dave ---testcase.py--- import sys, urllib, htmllib def Checkit(URL): try: print "Opening", URL
1
by: William Starr Moake | last post by:
Another problem with the browser-based WYSIWYG editor I'm trying to assemble using IE's design mode. This opens the selected page in the editor iframe named iView: <select name="template"...
3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
5
by: Mr Gordonz | last post by:
Hi all, I want to put a button on a page, and when the user clicks it, the standard Windows "Open File" dialogue box opens, and the user can browse/select a file on their PC. Having selected a...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
4
by: Wayne | last post by:
How do I get rid of the generic Windows "Open File - Security Warning" that appears when I try to open a database that resides on another PC on my home network? This is not the annoying macro...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
1
by: snowbell | last post by:
I managed to open a file from C by using system("notepad eg.txt"); However, I realise that the file only opens up but does not pop up if the user has other windows in use. Is there any way to make...
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.