473,698 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream problem

Hello,
I have small clafification.

#include<fstrea m>
#include<iostre am>
using namespace std;

void main()
{
ofstream ofs;
ofs<<"before open"<<endl;
ofs.open("\\tes t.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 3446
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*******@gmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hello,
I have small clafification.

#include<fstrea m>
#include<iostre am>
using namespace std;

void main()
{
ofstream ofs;
ofs<<"before open"<<endl;
ofs.open("\\tes t.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*******@gmai l.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
| Hello,
| I have small clafification.
|
| #include<fstrea m>
| #include<iostre am>
| 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("\\tes t.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<fstrea m>
#include<iostre am>
using namespace std;

void main()
{
ofstream ofs;
ofs<<"before open"<<endl;
ofs.open("\\tes t.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
2510
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 files altogether. Why doesn't it create files consistently? (My disk isn't full, and the file didn't previously exist) Thanks for your help. sample code: ------------------ ofstream outfile;
3
7824
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 for the screen. Right now the two declarations are: std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass); std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
2
448
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 did below: char filename = {"quest.01.cpp","quest.02.cpp","quest.03.cpp","quest.04.cpp","quest.05.cpp"};
5
18594
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
2225
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, std::string> When I run the code below I see the TTK_LOG( our logging function) call correctly outputting all the values in the matrix. However I never see anything output to the ofstream. (there's other data put into the ofstream object elsewhere...
5
14681
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
2395
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 get an Access Violation. Here is some of the code w/ comments as to what isn't working... void cpApp::write_to_file(string w){ char first = *w.begin(); map<char, ofstream*>::iterator i = stream_map.find(first); map<char,...
4
4740
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 doesn't work. Here's some code fragments... class matrix { public: ...... friend ostream& operator << ( ostream& os, matrix& m );
5
3776
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 in total. how do I go about it?, do I set a vector<ofstream*cities; something like
4
3023
by: rince | last post by:
Hi! I have a complex class like class Complex { double re; double im; public: Complex();
0
8598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.