Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ofstream

Reply
Thread Tools

ofstream

 
 
Gurikar
Guest
Posts: n/a
 
      05-12-2005
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

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      05-12-2005
"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
codigo
Guest
Posts: n/a
 
      05-12-2005

"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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: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: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.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      05-12-2005

"Gurikar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-13-2005
codigo wrote:
> "Gurikar" <(E-Mail Removed)> 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'.

 
Reply With Quote
 
Gurikar
Guest
Posts: n/a
 
      05-13-2005

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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      05-13-2005
On 12 May 2005 20:48:31 -0700, "Gurikar" <(E-Mail Removed)> 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
 
Reply With Quote
 
Mike Austin
Guest
Posts: n/a
 
      05-13-2005
Jack Klein wrote:
> On 12 May 2005 20:48:31 -0700, "Gurikar" <(E-Mail Removed)> 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
 
Reply With Quote
 
codigo
Guest
Posts: n/a
 
      05-13-2005

"Old Wolf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> codigo wrote:
> > "Gurikar" <(E-Mail Removed)> 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.


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      05-13-2005
"Mike Austin" <(E-Mail Removed)> wrote in message
news:LrXge.208057$(E-Mail Removed)
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extending ofstream class? Lars Yencken C++ 3 05-22-2013 11:52 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM
File cannot open with ofstream constructor ! tornado C++ 1 07-29-2003 04:38 PM
help: ofstream doesn't always create files? james545@my-deja.com C++ 0 07-16-2003 02:27 PM
writes to files passed as struct of ofstream objects fail m vaughn C++ 0 07-11-2003 01:06 AM



Advertisments