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

ofstream problem

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

Jul 23 '05 #1
3 3408
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" <ms*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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

Jul 23 '05 #2

"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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
Jul 23 '05 #3
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

Jul 23 '05 #4

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

Similar topics

0
by: james545 | last post by:
I am having a problem with ofstream creating files. Sometimes when I run the program, ofstream creates a new file for writing and writes to it. No problem. But sometimes it fails to create the...
3
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
2
by: bob | last post by:
Hi, I'm working with an ofstream object and using it to print the contents on an 100 * 100 matrix. i.e. lots of elements. the _matrix variable below is defined as std::vector<std::string,...
5
by: shyam | last post by:
HI I have a logging application wherin I create a ofstream object say, ofstream logger; by doing this logger.open(/* some path*/,ios_base:app);
1
by: ictheion | last post by:
Hi, I am having a problem utilizing ofstream*'s stored in a map (map<char, ofstream*>). I'm having basically the same problem with a map<char, boost::mutex*>. My code compiles, but at run-time I...
4
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
4
by: rince | last post by:
Hi! I have a complex class like class Complex { double re; double im; public: Complex();
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.