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

a simple ofstream open question

Hi,

If I write the following code:

ofstream f;
f.open("c:\\hello.txt", ios::binary | ios::out);

how do I check if the file is successfully opened and ready to write ?
Should I do a f.is_open() check ?

Regards,

...ab
Sep 11 '07 #1
3 1990
how do I check if the file is successfully opened and ready to write ?

Try:
if ( f.good() )
{
// Write...
}

"Abubakar" <s@s.comwrote in message
news:O6**************@TK2MSFTNGP05.phx.gbl...
Hi,

If I write the following code:

ofstream f;
f.open("c:\\hello.txt", ios::binary | ios::out);

how do I check if the file is successfully opened and ready to write ?
Should I do a f.is_open() check ?

Regards,

..ab


Sep 11 '07 #2
Thanks, n what if write() function fails.

...ab

"Michael Phillips, Jr." <mp*********@nospam.jun0.c0mwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>how do I check if the file is successfully opened and ready to write ?

Try:
if ( f.good() )
{
// Write...
}

"Abubakar" <s@s.comwrote in message
news:O6**************@TK2MSFTNGP05.phx.gbl...
>Hi,

If I write the following code:

ofstream f;
f.open("c:\\hello.txt", ios::binary | ios::out);

how do I check if the file is successfully opened and ready to write ?
Should I do a f.is_open() check ?

Regards,

..ab



Sep 11 '07 #3
Thanks, n what if write() function fails.

Then f.fail() will return true.

"Abubakar" <s@s.comwrote in message
news:ex**************@TK2MSFTNGP03.phx.gbl...
Thanks, n what if write() function fails.

..ab

"Michael Phillips, Jr." <mp*********@nospam.jun0.c0mwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>how do I check if the file is successfully opened and ready to write ?

Try:
if ( f.good() )
{
// Write...
}

"Abubakar" <s@s.comwrote in message
news:O6**************@TK2MSFTNGP05.phx.gbl...
>>Hi,

If I write the following code:

ofstream f;
f.open("c:\\hello.txt", ios::binary | ios::out);

how do I check if the file is successfully opened and ready to write ?
Should I do a f.is_open() check ?

Regards,

..ab




Sep 11 '07 #4

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

Similar topics

1
by: tornado | last post by:
hi all, This is quite puzzling me why is that ofstream constructor is unable to open a file for writing while, a ofstream "open" method can ! The c++ book by Herbert Schildt which i m readin...
1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
4
by: Tom Johnson | last post by:
Hi all, I cant believe that Im stuck on such simple code, but I am so... Im trying to open a text file for writing but can never get the file to initially open. Heres what Im having trouble with....
1
by: phoenix | last post by:
How can i read a numbers from file.. I created this file like this: int dane, i; std::ofstream plik_z; plik_z.open(nazwa, std::ios::out | std::ios::trunc | std::ios::binary); for (i = 0; i <...
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
11
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.