ofstream

Discussion in 'C++' started by Gurikar, May 12, 2005.

  1. Gurikar

    Gurikar Guest

    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
     
    Gurikar, May 12, 2005
    #1
    1. Advertising

  2. Gurikar

    John Carson Guest

    "Gurikar" <> wrote in message
    news:
    > 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
     
    John Carson, May 12, 2005
    #2
    1. Advertising

  3. Gurikar

    codigo Guest

    "Gurikar" <> wrote in message
    news:...
    > 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.
     
    codigo, May 12, 2005
    #3
  4. Gurikar

    Mike Wahler Guest

    "Gurikar" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, May 12, 2005
    #4
  5. Gurikar

    Old Wolf Guest

    codigo wrote:
    > "Gurikar" <> 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'.
     
    Old Wolf, May 13, 2005
    #5
  6. Gurikar

    Gurikar Guest

    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
     
    Gurikar, May 13, 2005
    #6
  7. Gurikar

    Jack Klein Guest

    On 12 May 2005 20:48:31 -0700, "Gurikar" <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 13, 2005
    #7
  8. Gurikar

    Mike Austin Guest

    Jack Klein wrote:
    > On 12 May 2005 20:48:31 -0700, "Gurikar" <> 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
     
    Mike Austin, May 13, 2005
    #8
  9. Gurikar

    codigo Guest

    "Old Wolf" <> wrote in message
    news:...
    > codigo wrote:
    > > "Gurikar" <> 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.
     
    codigo, May 13, 2005
    #9
  10. Gurikar

    John Carson Guest

    "Mike Austin" <> wrote in message
    news:LrXge.208057$
    > 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
     
    John Carson, May 13, 2005
    #10
  11. Gurikar

    Mike Austin Guest

    John Carson wrote:
    > "Mike Austin" <> wrote in message
    > news:LrXge.208057$
    >
    >> 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
     
    Mike Austin, May 13, 2005
    #11
  12. 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
     
    Matthias Kaeppler, May 15, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. m vaughn
    Replies:
    0
    Views:
    431
    m vaughn
    Jul 11, 2003
  2. Replies:
    0
    Views:
    630
  3. tornado
    Replies:
    1
    Views:
    784
    Mike Wahler
    Jul 29, 2003
  4. Lars Yencken

    Extending ofstream class?

    Lars Yencken, Aug 14, 2003, in forum: C++
    Replies:
    3
    Views:
    1,056
    Zaphod
    May 23, 2013
  5. Squid Seven

    ofstream * vs. ofstream

    Squid Seven, Jul 13, 2005, in forum: C++
    Replies:
    5
    Views:
    614
    Ivan Johansen
    Jul 14, 2005
Loading...

Share This Page