473,322 Members | 1,493 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,322 software developers and data experts.

Why is stream fail() bit set?


This thing is driving me nuts.

why would a simple output file stream like this:

ofstream ofs;
ofs.open("c:\temp\somefile.txt")

set the fail bit?

calling ofs.fail() right after the open is returning true.

Oct 26 '06 #1
18 4350
Hi,

\t
means tabstop
use either
c:/temp/somefile.txt
or
c:\\temp\\somefile.txt

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
This thing is driving me nuts.

why would a simple output file stream like this:

ofstream ofs;
ofs.open("c:\temp\somefile.txt")

set the fail bit?

calling ofs.fail() right after the open is returning true.

Oct 26 '06 #2

Moonlit wrote:
Hi,

\t
means tabstop
use either
c:/temp/somefile.txt
or
c:\\temp\\somefile.txt
That was just an example. I know a backslash inside a string needs to
be escaped. The file does get created. Its just that I can't write
anythng into it because fail() returns true and I can't figure out why.
The low level OS _open (or whatever) call seems to successfully create
the file everytime. Somewhere after that I don't know why that bit is
getting set.

Oct 26 '06 #3
Dilip wrote:
This thing is driving me nuts.

why would a simple output file stream like this:

ofstream ofs;
ofs.open("c:\temp\somefile.txt")
Will Windoze let you put tabs in a file name? Remember
\ is special in C++.
Oct 26 '06 #4
Hi,

You should post the code that you used not something you made up.

The code you posted doesn't work neither on windows nor unix/linux

If you want help post your complete code cut and paste.

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Moonlit wrote:
>Hi,

\t
means tabstop
use either
c:/temp/somefile.txt
or
c:\\temp\\somefile.txt

That was just an example. I know a backslash inside a string needs to
be escaped. The file does get created. Its just that I can't write
anythng into it because fail() returns true and I can't figure out why.
The low level OS _open (or whatever) call seems to successfully create
the file everytime. Somewhere after that I don't know why that bit is
getting set.

Oct 26 '06 #5

Dilip wrote:
This thing is driving me nuts.

why would a simple output file stream like this:

ofstream ofs;
ofs.open("c:\temp\somefile.txt")

set the fail bit?

calling ofs.fail() right after the open is returning true.
If you create the file successfully, assuming the OS perimissions are
set appropritely, you should be able to stream to the file. But since
you aren't showing how you do that, nobody can help.

Oct 26 '06 #6
On Oct 26, 5:54 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
Dilip wrote:
This thing is driving me nuts.
why would a simple output file stream like this:
ofstream ofs;
ofs.open("c:\temp\somefile.txt")
set the fail bit?
calling ofs.fail() right after the open is returning true.If you create the file successfully, assuming the OS perimissions are
set appropritely, you should be able to stream to the file. But since
you aren't showing how you do that, nobody can help.
I am not streaming to the file yet. Before I decide to do that I do a
check to see if the stream is in usable state by calling ofs.fail().
Because fail() is returning true I am not going ahead with the
streaming. I am trying to figure why fail() returns true when the file
got created successfully.

Oct 27 '06 #7


On Oct 26, 5:30 pm, Ron Natalie <r...@spamcop.netwrote:
Dilip wrote:
This thing is driving me nuts.
why would a simple output file stream like this:
ofstream ofs;
ofs.open("c:\temp\somefile.txt")Will Windoze let you put tabs in a file name? Remember
\ is special in C++.
That was just a typo in the example. The real code gets the file name
from elsewhere. As I repeatedly stated in other threads the file
*does* get created successfully right after the .open call. I just
don't understand why fail() on the stream returns true after that.

Oct 27 '06 #8
On 26 Oct 2006 15:27:20 -0700 in comp.lang.c++, "Dilip"
<rd*****@lycos.comwrote,
>That was just an example. I know a backslash inside a string needs to
be escaped.
Then it's not a very good example, is it?
The file does get created. Its just that I can't write
anythng into it because fail() returns true and I can't figure out why.
What does perror() say?

Oct 27 '06 #9
On Oct 26, 7:36 pm, David Harmon <sou...@netcom.comwrote:
On 26 Oct 2006 15:27:20 -0700 in comp.lang.c++, "Dilip"
<rdil...@lycos.comwrote,
The file does get created. Its just that I can't write
anythng into it because fail() returns true and I can't figure out why.
What does perror() say?
Doesn't perror write to stderr? My app is not a console application.
Is there any other way to check? Or should I redirect stderr to
elsewhere and track down the error message? That seems awfully
complicated.

Oct 27 '06 #10
On 26 Oct 2006 17:52:11 -0700 in comp.lang.c++, "Dilip"
<rd*****@lycos.comwrote,
>What does perror() say?

Doesn't perror write to stderr? My app is not a console application.
Is there any other way to check?
strerror() returns the same message string.
Oct 27 '06 #11

"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
On Oct 26, 5:54 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
>Dilip wrote:
This thing is driving me nuts.
why would a simple output file stream like this:
ofstream ofs;
ofs.open("c:\temp\somefile.txt")
set the fail bit?
calling ofs.fail() right after the open is returning true.If you create
the file successfully, assuming the OS perimissions are
set appropritely, you should be able to stream to the file. But since
you aren't showing how you do that, nobody can help.

I am not streaming to the file yet. Before I decide to do that I do a
check to see if the stream is in usable state by calling ofs.fail().
Because fail() is returning true I am not going ahead with the
streaming. I am trying to figure why fail() returns true when the file
got created successfully.
Why are you making it difficult for anyone to help you?
Don't *describe* the code, *show* it to us (preferably
a trimmed-down version that still exhibits the problem).
Failing that, we can only guess ...

-Mike
Oct 27 '06 #12
On Oct 26, 9:00 pm, David Harmon <sou...@netcom.comwrote:
On 26 Oct 2006 17:52:11 -0700 in comp.lang.c++, "Dilip"
<rdil...@lycos.comwrote,
What does perror() say?
Doesn't perror write to stderr? My app is not a console application.
Is there any other way to check?strerror() returns the same message string.
_strerror(NULL) returns "No Error". So why would the stream return
true for fail()? Since everyone seems to be clamoring to repeat my
paltry 3 lines of code, I am doing it here again:

std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl; // returns true but file gets
created successfully

Oct 27 '06 #13
VJ
Dilip wrote:
On Oct 26, 9:00 pm, David Harmon <sou...@netcom.comwrote:
>>On 26 Oct 2006 17:52:11 -0700 in comp.lang.c++, "Dilip"
<rdil...@lycos.comwrote,

>>>>What does perror() say?
>>>Doesn't perror write to stderr? My app is not a console application.
Is there any other way to check?strerror() returns the same message string.


_strerror(NULL) returns "No Error". So why would the stream return
true for fail()? Since everyone seems to be clamoring to repeat my
paltry 3 lines of code, I am doing it here again:

std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl; // returns true but file gets
created successfully
Sorry to disapoint you, but my program prints false as a result on
ofs.fail() :
#include <iostream>
#include <fstream>
#include <string>
main()
{
std::string fileNameIGetFromRegistry( "abcabc.txt" );
std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl;

}
Oct 27 '06 #14

Dilip wrote:
On Oct 26, 9:00 pm, David Harmon <sou...@netcom.comwrote:
On 26 Oct 2006 17:52:11 -0700 in comp.lang.c++, "Dilip"
<rdil...@lycos.comwrote,
>What does perror() say?
>Doesn't perror write to stderr? My app is not a console application.
>Is there any other way to check?strerror() returns the same message string.

_strerror(NULL) returns "No Error". So why would the stream return
true for fail()? Since everyone seems to be clamoring to repeat my
paltry 3 lines of code, I am doing it here again:

std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl; // returns true but file gets
created successfully
As it seems that we are all unable to reproduce your error and as you
fail to give a complete, compileable program we are unable to help you.
Create a minimal program that reproduces your error and show it to us.
In that case we might be able to help you.

/Peter

Oct 27 '06 #15
Dilip wrote:
On Oct 26, 9:00 pm, David Harmon <sou...@netcom.comwrote:
>On 26 Oct 2006 17:52:11 -0700 in comp.lang.c++, "Dilip"
<rdil...@lycos.comwrote,
>>>What does perror() say?
Doesn't perror write to stderr? My app is not a console application.
Is there any other way to check?strerror() returns the same message string.

_strerror(NULL) returns "No Error". So why would the stream return
true for fail()? Since everyone seems to be clamoring to repeat my
paltry 3 lines of code, I am doing it here again:

std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl; // returns true but file gets
created successfully
1. _strerror() is not part of the Standard, strerror() [without
a leading underscore] is.

2. strerror() does not take the same argument list as perror().

#include <cerrno>
#include <cstring>

const char *error_str = std::strerror(errno);

Try that.
Oct 27 '06 #16
Mike Wahler wrote:
"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@f16g2000cwb.googlegr oups.com...
>On Oct 26, 5:54 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
>>Dilip wrote:
This thing is driving me nuts.
why would a simple output file stream like this:
ofstream ofs;
ofs.open("c:\temp\somefile.txt")
set the fail bit?
calling ofs.fail() right after the open is returning true.If you create
the file successfully, assuming the OS perimissions are
set appropritely, you should be able to stream to the file. But since
you aren't showing how you do that, nobody can help.
I am not streaming to the file yet. Before I decide to do that I do a
check to see if the stream is in usable state by calling ofs.fail().
Because fail() is returning true I am not going ahead with the
streaming. I am trying to figure why fail() returns true when the file
got created successfully.

Why are you making it difficult for anyone to help you?
Don't *describe* the code, *show* it to us (preferably
a trimmed-down version that still exhibits the problem).
Failing that, we can only guess ...
OP's problem is most likely that he's not escaping his backslashes, i.e.

ofs.open("C:\\temp\\somefile.txt");
Oct 27 '06 #17

"red floyd" <no*****@here.dudewrote in message
news:3%****************@newssvr14.news.prodigy.com ...
Mike Wahler wrote:
>"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@f16g2000cwb.googleg roups.com...
>>On Oct 26, 5:54 pm, "Salt_Peter" <pj_h...@yahoo.comwrote:
Dilip wrote:
This thing is driving me nuts.
why would a simple output file stream like this:
ofstream ofs;
ofs.open("c:\temp\somefile.txt")
set the fail bit?
calling ofs.fail() right after the open is returning true.If you
create the file successfully, assuming the OS perimissions are
set appropritely, you should be able to stream to the file. But since
you aren't showing how you do that, nobody can help.
I am not streaming to the file yet. Before I decide to do that I do a
check to see if the stream is in usable state by calling ofs.fail().
Because fail() is returning true I am not going ahead with the
streaming. I am trying to figure why fail() returns true when the file
got created successfully.

Why are you making it difficult for anyone to help you?
Don't *describe* the code, *show* it to us (preferably
a trimmed-down version that still exhibits the problem).
Failing that, we can only guess ...

OP's problem is most likely that he's not escaping his backslashes, i.e.

ofs.open("C:\\temp\\somefile.txt");
When that was mentioned, he said he already knew about that issue,
and that wasn't it. See his first reply to the first reply.

-Mike
Oct 27 '06 #18

Dilip wrote in message
>
_strerror(NULL) returns "No Error". So why would the stream return
true for fail()? Since everyone seems to be clamoring to repeat my
paltry 3 lines of code, I am doing it here again:

std::ofstream ofs;
ofs.open(fileNameIGetFromRegistry.c_str());
std::cout << ofs.fail() << std::endl; // returns true but file gets
created successfully
#include <string>
#include <iostream>
#include <ostream>
#include <fstream>

void FixPath( std::string &Path){
#ifdef MAX_PATH // windows: #define MAX_PATH 260
if( Path.size() >= MAX_PATH ){ Path = "";} //too long
#endif
char Quote('\"');
if( Path.find(" ") != Path.npos){
if( Path.at(0) != Quote ){
Path.insert(0, 1, Quote );
}
if( Path.at(Path.size()-1) != Quote ){
Path.push_back( Quote );
}
} // spaces found
return;
} // FixPath(string&)

int main(){

// get string "fileNameIGetFromRegistry"

std::string FilePath( fileNameIGetFromRegistry );
std::cout << FilePath << std::endl;
FixPath( FilePath );
std::cout << FilePath << std::endl;

// are the two strings printed the same?

std::ofstream ofs( FilePath.c_str() );
if( not ofs ){
std::cout<<" file error="<<ofs.flags()<<std::endl;
std::cout<<" ios::good="<<ofs.good()<<std::endl;
std::cout<<" ios::bad="<<ofs.bad()<<std::endl;
std::cout<<" ios::eof="<<ofs.eof()<<std::endl;
std::cout<<" ios::fail="<<ofs.fail()<<std::endl;
}
} // main()

Post your output.

--
Bob R
POVrookie
Oct 27 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jason Heyes | last post by:
I have a function whose job is to read a non-negative integer. Is this the right way to do it? std::istream &read_integer(std::istream &is, int &i) { if (!(is >> i) || i < 0)...
15
by: Adam H. Peterson | last post by:
I would like to make a stream or streambuf that tracks the number of lines that have been read and stuff like that (so, for example, when I get an error message, I can ask the stream for the line...
5
by: ad | last post by:
I used use SharpZipLib to compress files in disk. But now I want to compress stream into another stream in memory(the stream not associated with disk file) My pseudo is: Stream...
1
by: Robbie Hatley | last post by:
Say I have an ifstream object, like so: #include <iostream> #include <fstream> int main(int, char* Sam) { std::ifstream Bob; Bob.open(Sam); std::string buffer; while (42)
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
0
by: heplesser | last post by:
Summary: Does the C++ standard require std::basic_istream<>::operator>>(double&) to leave the input stream untouched in case of a read failure? Details: I noticed an unexpected behavior of...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.