ofstream

G

Gurikar

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
 
J

John Carson

Gurikar said:
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?
 
C

codigo

Gurikar said:
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::eek:fstream& 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::eek:fstream 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.
 
M

Mike Wahler

Gurikar said:

#include <cstdlib>
#include <fstream>
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
 
O

Old Wolf

codigo said:
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'.
 
G

Gurikar

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
 
J

Jack Klein

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.
 
M

Mike Austin

Jack said:
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
 
C

codigo

Old Wolf said:
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.
 
J

John Carson

Mike Austin said:
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.
 
M

Mike Austin

John said:
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
 
M

Matthias Kaeppler

Mike said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top