472,378 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 3628
"Gurikar" <ms*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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::ofstream& ofs, const std::string s)
{
ofs << s << std::endl;
}

int main()
{
bool b_havefun = false;

// output file stream
std::string s_file("text.txt");
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*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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*******@gmail.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"<<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"<<endl many times affect performance without opening
file( i mean is performancei is same as withour using
ofs<<"hello"<<endl in code.)

Regards

Jul 23 '05 #6
On 12 May 2005 20:48:31 -0700, "Gurikar" <ms*******@gmail.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"<<endl 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.

--
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.learn.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*******@gmail.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"<<endl 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*****@inspire.net.nz> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
codigo wrote:
"Gurikar" <ms*******@gmail.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********************@bgtnsc04-news.ops.worldnet.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
John Carson wrote:
"Mike Austin" <no@spam.com> wrote in message
news:Lr********************@bgtnsc04-news.ops.worldnet.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.


I would expect it to throw an exception, not simply be undefined
behavior. Or better yet, let it be configurable - throw an exception
or let it slide. But please, don't let it be undefined.
Mike
Jul 23 '05 #11
Mike Austin wrote:
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.

I would expect it to throw an exception, not simply be undefined
behavior. Or better yet, let it be configurable - throw an exception
or let it slide. But please, don't let it be undefined.
Mike


There is a name for what you're looking for: It's called Java. It has
all those exceptions and you always pay for them, even if you don't want
them. C++ doesn't, and it can indeed produce unsafer code which is more
prone to errors. But under the line, it's exactly these things which
make C++ slim and fast, and Java big and slow.
So if you want an exception to be thrown, then test for failure and do so.

--
Matthias Kaeppler
Jul 23 '05 #12

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

Similar topics

1
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...
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...
5
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...
2
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();...
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 {
5
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);...
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...
5
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...
15
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() .......
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.