Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream problem

Reply
Thread Tools

ofstream problem

 
 
Gurikar
Guest
Posts: n/a
 
      05-06-2005
Hello,
I have small clafification.

#include<fstream>
#include<iostream>
using namespace std;

void main()
{
ofstream ofs;
ofs<<"before open"<<endl;
ofs.open("\\test.txt");
ofs<<"Test File"<<endl;
ofs.close();
}

when i do this, there is nothing in test.ext file. I do agree that iam
writing something using ofstream before creating file test.txt, but it
should give error. Even after that, iam creating file and opened, when
i write something after, nothing is getting written in that file. It
justs empty? Why is this behaviuor??

Regards

 
Reply With Quote
 
 
 
 
cadull
Guest
Posts: n/a
 
      05-06-2005
I think you will find that ofs.fail() will return true. To enable further
use of the stream call ofs.clear(). This will reset the error flags.

Regards,
cadull

"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello,
> I have small clafification.
>
> #include<fstream>
> #include<iostream>
> using namespace std;
>
> void main()
> {
> ofstream ofs;
> ofs<<"before open"<<endl;
> ofs.open("\\test.txt");
> ofs<<"Test File"<<endl;
> ofs.close();
> }
>
> when i do this, there is nothing in test.ext file. I do agree that iam
> writing something using ofstream before creating file test.txt, but it
> should give error. Even after that, iam creating file and opened, when
> i write something after, nothing is getting written in that file. It
> justs empty? Why is this behaviuor??
>
> Regards
>



 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      05-06-2005

"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
| Hello,
| I have small clafification.
|
| #include<fstream>
| #include<iostream>
| using namespace std;
|
| void main()

You should be using a return type of int for the
signature of main().

| {
| ofstream ofs;
| ofs<<"before open"<<endl;

You have just broken the stream state, and it will need
to be reset with the member 'clear()'.

| ofs.open("\\test.txt");
| ofs<<"Test File"<<endl;
| ofs.close();
| }
|
| when i do this, there is nothing in test.ext file. I do agree that iam
| writing something using ofstream before creating file test.txt, but it
| should give error. Even after that, iam creating file and opened, when
| i write something after, nothing is getting written in that file. It
| justs empty? Why is this behaviuor??

Because you broke the stream, and you cannot open a file
with a broken stream until you repair it (reset the stream
state flags), with clear().

Cheers,
Chris Val


 
Reply With Quote
 
Kristo
Guest
Posts: n/a
 
      05-06-2005
Gurikar wrote:
> Hello,
> I have small clafification.
>
> #include<fstream>
> #include<iostream>
> using namespace std;
>
> void main()
> {
> ofstream ofs;
> ofs<<"before open"<<endl;
> ofs.open("\\test.txt");
> ofs<<"Test File"<<endl;
> ofs.close();
> }
>
> when i do this, there is nothing in test.ext file. I do agree that
> iam writing something using ofstream before creating file test.txt,
> but it should give error.


No, it shouldn't. The standard says that if an error occurs while
writing (e.g., writing to a file that isn't open) it sets failbit.
There is no mention of an error message.

> Even after that, iam creating file and opened, when i write something
> after, nothing is getting written in that file. It justs empty? Why
> is this behaviuor??


Once failbit has been set, anything you do with a stream object will
also fail. For this reason, always check that open didn't fail before
you write to a file.

Kristo

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
ofstream problem opening file Alfons C++ 1 02-06-2005 08:00 PM
ofstream problem slyphiad C++ 2 09-14-2004 03:41 AM
Problem which ifstream and ofstream class in VC++ Armando C++ 2 01-23-2004 03:50 PM
seemingly simple ofstream problem please help Tom Johnson C++ 4 08-15-2003 10:49 PM



Advertisments