473,714 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ofstream

Hello,

ofstream ofs;

fun(char* str)
{
ofs<<str<<endl;
}

void main()
{

int i=0;
....
....

if(i)
{
ofs.open("text. txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}

}

In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???

Regards

Jul 23 '05 #1
11 3824
"Gurikar" <ms*******@gmai l.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com
Hello,

ofstream ofs;

fun(char* str)
{
ofs<<str<<endl;
}

void main()
{

int i=0;
...
...

if(i)
{
ofs.open("text. txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}

}

In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???

Regards

Here I am, banging my head with a hammer. What happens here?

--
John Carson
Jul 23 '05 #2

"Gurikar" <ms*******@gmai l.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello,

ofstream ofs;

fun(char* str)
{
ofs<<str<<endl;
}

void main()
{

int i=0;
...
...

if(i)
{
ofs.open("text. txt");
fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}

}

In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???


Who knows, the above code is not C++. Neither would it compile on any
compiler(c++ or not). fun() has no return type, ofs in fun() is not defined.
main must return an integer. You need to read up on scopes and lifetime of
objects.

try something like:

#include <iostream>
#include <fstream>
#include <string>

void fun(std::ofstre am& ofs, const std::string s)
{
ofs << s << std::endl;
}

int main()
{
bool b_havefun = false;

// output file stream
std::string s_file("text.tx t");
std::ofstream ofs;
ofs.open(s_file .c_str());
if (!ofs)
{
std::cout << "error while opening " << s_file << std::endl;
}

if (b_havefun)
{
fun(ofs, "lets party");
}
else
{
fun(ofs, "off to work");
}

return 0;
}

Write-protect the text.txt file to see the error message.
Jul 23 '05 #3

"Gurikar" <ms*******@gmai l.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Hello,

#include <cstdlib>
#include <fstream>
#include <ostream>
using namespace std;
ofstream ofs;

fun(char* str)
Return type required. e.g.:

void fun(char *str)
{
ofs<<str<<endl;
}

void main()
'main()' is required to have return type of 'int'.

int main()
{

int i=0;
...
...

if(i)
{
ofs.open("text. txt");
You should check whether the open succeeded or failed.

if(!ofs)
{
cerr << "can't open file\n";
return EXIT_FAILURE;
}

fun("heih0");
}
else
{
// Here iam not opening file
fun("heih0");
}

}

In the above code, in else condition iam not opening file, but still
calling fun(), What happens here???


The call to ofs<< will fail.
-Mike
Jul 23 '05 #4
codigo wrote:
"Gurikar" <ms*******@gmai l.com> wrote:

ofstream ofs;

fun(char* str)
{
ofs<<str<<endl;
}


The above code is not C++. Neither would it compile on any
compiler(c++ or not). fun() has no return type, ofs in fun()
is not defined. main must return an integer. You need to read
up on scopes and lifetime of objects.


It's you who needs to read up on scopes: 'ofs' in fun()
correctly refers to the global (file-scope) variable 'ofs'.

Jul 23 '05 #5

ofstream ofs;
ofs<<"hello"<<e ndl;

here i have not opened file, iam able to use this, only thing file wont
get created(its like dummy). I checked it, its running file, ofs value
is NULL. But i want to know does it affect performance. Actually i dont
want to write in a file in some situation so i wont open file, in some
case i want write a file, so i open a file. and call this. So just
using ofs<<"hello"<<e ndl many times affect performance without opening
file( i mean is performancei is same as withour using
ofs<<"hello"<<e ndl in code.)

Regards

Jul 23 '05 #6
On 12 May 2005 20:48:31 -0700, "Gurikar" <ms*******@gmai l.com> wrote
in comp.lang.c++:

ofstream ofs;
ofs<<"hello"<<e ndl;

here i have not opened file, iam able to use this, only thing file wont
get created(its like dummy). I checked it, its running file, ofs value
is NULL. But i want to know does it affect performance. Actually i dont
want to write in a file in some situation so i wont open file, in some
case i want write a file, so i open a file. and call this. So just
using ofs<<"hello"<<e ndl many times affect performance without opening
file( i mean is performancei is same as withour using
ofs<<"hello"<<e ndl in code.)

Regards


Writing to an uninitialized stream is completely undefined behavior.
It could do nothing. It could take 100 times as long as writing to a
real file. It could crash your computer or corrupt files on your disk
drive.

It is undefined behavior and the C++ language neither knows nor cares
what it does, once you generate undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #7
Jack Klein wrote:
On 12 May 2005 20:48:31 -0700, "Gurikar" <ms*******@gmai l.com> wrote
in comp.lang.c++:

ofstream ofs;
ofs<<"hello"< <endl;

here i have not opened file, iam able to use this, only thing file wont
get created(its like dummy). I checked it, its running file, ofs value
is NULL. But i want to know does it affect performance. Actually i dont
want to write in a file in some situation so i wont open file, in some
case i want write a file, so i open a file. and call this. So just
using ofs<<"hello"<<e ndl many times affect performance without opening
file( i mean is performancei is same as withour using
ofs<<"hello"< <endl in code.)

Regards

Writing to an uninitialized stream is completely undefined behavior.
It could do nothing. It could take 100 times as long as writing to a
real file. It could crash your computer or corrupt files on your disk
drive.

It is undefined behavior and the C++ language neither knows nor cares
what it does, once you generate undefined behavior.


Mark another -1 for C++. C++ has so many undefined behaviors, gotchas
and unintuitive constructs that it makes it very difficult to get things
done sometimes. I've been using C++ for over 10 years, and always seem
to stumble on something. It seems to be the standard libraries that
cause the issues actually, not the language.

Mike
Jul 23 '05 #8

"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
codigo wrote:
"Gurikar" <ms*******@gmai l.com> wrote:

ofstream ofs;

fun(char* str)
{
ofs<<str<<endl;
}


The above code is not C++. Neither would it compile on any
compiler(c++ or not). fun() has no return type, ofs in fun()
is not defined. main must return an integer. You need to read
up on scopes and lifetime of objects.


It's you who needs to read up on scopes: 'ofs' in fun()
correctly refers to the global (file-scope) variable 'ofs'.


indeed, i didn't see it.
Jul 23 '05 #9
"Mike Austin" <no@spam.com> wrote in message
news:Lr******** ************@bg tnsc04-news.ops.worldn et.att.net
Jack Klein wrote:


Writing to an uninitialized stream is completely undefined behavior.
It could do nothing. It could take 100 times as long as writing to a
real file. It could crash your computer or corrupt files on your
disk drive.

It is undefined behavior and the C++ language neither knows nor cares
what it does, once you generate undefined behavior.


Mark another -1 for C++. C++ has so many undefined behaviors, gotchas
and unintuitive constructs that it makes it very difficult to get
things done sometimes. I've been using C++ for over 10 years, and
always seem to stumble on something. It seems to be the standard
libraries that cause the issues actually, not the language.

You think it is unreasonable to expect that files be opened before writing
to them? Or that if you do "write to them" without them being opened, then
the consequences are undefined? Unintuitive? Pretty damned obvious I'd say.

--
John Carson

Jul 23 '05 #10

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

Similar topics

1
11430
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 ifstream and fstream as well, but I'm typing on the fly) yes, I know the syntax may be off... class onamedfstream : public ofstream {
3
7828
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);
5
2656
by: cpp | last post by:
When I create an instance of ofstream, what is the name of the member variable that holds the filename? For example: ofstream ofs("Output.txt"); cout << ofs.WhatIsThePathVariable; If there isn't a public member variable I can use, then is there at least a function that displays the filename?
2
2987
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str(); ofstream out0(open_output, ios::out | ios::out);
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
18601
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 {
5
5152
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); ofile.write(buffer,sizeof(buffer)); ofile.close(); }
5
3777
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
5
11417
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind of" an output file, there should be someway to do it. The following code shows what I am trying to do. ------------------------------------------------ #include <iostream> #include <fstream>
15
6007
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() .... std::ofstream fout; fout.open("hello.out"); fout<<setfill('-')<<setw(20)<<"-"<<setfill(' ')<<endl; fout<<"hello "; // this is line 139
0
8795
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
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
9306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9009
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
5943
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
4462
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
3155
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
2510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2103
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.