473,320 Members | 2,052 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,320 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 3796
"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() .......
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.