I am not sure what else to try in order to address this issue. -
#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: -
[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
6 14172
Your code works for me. I used the same file names as you did but removed the "./" since I am using windows.
Like this: - 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.
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.
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.
I think you should create this two file: itest.txt and ftest.txt first.
Is that right? Thank you!
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: -
fstream mfile("./ftest.txt", fstream::in | fstream::out | fstream::trunc);
-
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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"...
|
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...
|
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...
|
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....
|
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...
|
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...
|
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...
|
by: Coffee Pot |
last post by:
Thanks for any advice.
~ CP
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| | |