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 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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
{
|
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,...
|
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);
|
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...
|
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...
|
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...
|
by: rince |
last post by:
Hi! I have a complex class like
class Complex
{
double re;
double im;
public:
Complex();
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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: 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...
|
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: 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,...
| |