File Open and Close

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Venkat

    File Open and Close

    Hi,

    I am using ifstream to open a file read it and then close it.

    Sample Code
    -------------------

    ifStream inFile("file1.t xt");
    .......
    .......
    .......
    inFile.Close();


    Just by issuing the statement inFile.Close() will close all the stream
    object or we need to any further statements in order to release the file
    completely(like releasing everything related to the file so it is available
    for further opening/reading something like inFile.Unlock() , inFile.Clear())

    I am getting problem like once i open and then read from the file and then
    close it, the application is crashing.

    regards,
    Venkat






  • Thomas Matthews

    #2
    Re: File Open and Close

    Venkat wrote:[color=blue]
    > Hi,
    >
    > I am using ifstream to open a file read it and then close it.
    >
    > Sample Code
    > -------------------
    >
    > ifStream inFile("file1.t xt");
    > ......
    > ......
    > ......
    > inFile.Close();
    >
    >
    > Just by issuing the statement inFile.Close() will close all the stream
    > object or we need to any further statements in order to release the file
    > completely(like releasing everything related to the file so it is available
    > for further opening/reading something like inFile.Unlock() , inFile.Clear())
    >
    > I am getting problem like once i open and then read from the file and then
    > close it, the application is crashing.
    >
    > regards,
    > Venkat[/color]

    1. The C++ language is case-sensitive. The ifstream method is "close"
    not "Close".

    2. Show us the code between opening and closing, the actual behavior and
    the expected behavior.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Evan Carew

      #3
      Re: File Open and Close

      Vencat,


      Venkat wrote:[color=blue]
      > Hi,
      >
      > I am using ifstream to open a file read it and then close it.
      >
      > Sample Code
      > -------------------
      >
      > ifStream inFile("file1.t xt");
      > ......
      > ......
      > ......
      > inFile.Close();[/color]
      Insofar as it goes, your code looks good. For instance, the following
      program compiles and runs fine for me:


      #include <fstream>

      int main(){
      ifstream inFile("file1.t xt");
      if (!inFile.good() ) cerr << "Problem opening file1.txt!" << endl;
      inFile.close();
      cout << "Success opening file1.txt!" << endl;
      return 0;
      }

      [color=blue]
      >
      >
      > Just by issuing the statement inFile.Close() will close all the stream
      > object or we need to any further statements in order to release the file
      > completely(like releasing everything related to the file so it is available
      > for further opening/reading something like inFile.Unlock() , inFile.Clear())
      >
      > I am getting problem like once i open and then read from the file and then
      > close it, the application is crashing.
      >[/color]
      It sounds like your code is suffering from another problem. Have you
      tried looking at the core file with ddd or some other tool?


      Evan Carew

      Comment

      • Venkat

        #4
        Re: File Open and Close

        Evan,

        "Evan Carew" <tempcarew@pobo x.com> wrote in message
        news:10087q8pms liv73@corp.supe rnews.com...[color=blue]
        > Vencat,[/color]
        [color=blue]
        > Insofar as it goes, your code looks good. For instance, the following
        > program compiles and runs fine for me:
        >
        >
        > #include <fstream>
        >
        > int main(){
        > ifstream inFile("file1.t xt");
        > if (!inFile.good() ) cerr << "Problem opening file1.txt!" << endl;
        > inFile.close();
        > cout << "Success opening file1.txt!" << endl;
        > return 0;
        > }[/color]

        The above code turns to me also. Infact the subsequent open and close of
        same file runs fine, but i am not able to get a return value from the
        function and the app is crashing,
        i guess we are not releasing or calling the destructor properly and it
        could be reason of the application is crashing.
        I want to know inFile.close() would call the neccessary destructor to
        release the reasource? or am i missing something?


        regards,
        Venkat








        Comment

        • Chris Theis

          #5
          Re: File Open and Close


          "Evan Carew" <tempcarew@pobo x.com> wrote in message
          news:10087q8pms liv73@corp.supe rnews.com...
          [SNIP][color=blue]
          > Insofar as it goes, your code looks good. For instance, the following
          > program compiles and runs fine for me:[/color]

          Sorry for knit picking but I doubt that the following program compiles as it
          is. You're at missing either the std:: scope in front of, for example, cerr
          or a

          using namespace std;

          declaration.
          [color=blue]
          >
          > #include <fstream>
          >
          > int main(){
          > ifstream inFile("file1.t xt");
          > if (!inFile.good() ) cerr << "Problem opening file1.txt!" << endl;[/color]

          Why not write

          if( !in ) cerr << "Problem opening file1.txt!" << endl;

          This saves you some typing and why not use the implicit conversion
          operations implemented by the stream classes for that reason.
          [color=blue]
          > inFile.close();
          > cout << "Success opening file1.txt!" << endl;
          > return 0;
          > }
          >[color=green]
          > >[/color][/color]
          [SNIP]
          [color=blue][color=green]
          > > I am getting problem like once i open and then read from the file and[/color][/color]
          then[color=blue][color=green]
          > > close it, the application is crashing.
          > >[/color][/color]

          To the OP: Show us more of your actual code as the problem seems to be
          located somewhere else.

          [SNIP]

          Regards
          Chris


          Comment

          • Venkat

            #6
            Re: File Open and Close

            Chris,

            "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message
            news:bu19uc$36r $1@sunnews.cern .ch...[color=blue]
            >
            > [SNIP]
            >[/color]
            Venkat Wrote:[color=blue][color=green][color=darkred]
            > > > I am getting problem like once i open and then read from the file[/color][/color][/color]
            and[color=blue]
            > then[color=green][color=darkred]
            > > > close it, the application is crashing.
            > > >[/color][/color]
            >
            > To the OP: Show us more of your actual code as the problem seems to be
            > located somewhere else.[/color]

            Here is the code.

            file prg1.cpp
            {
            ...
            ....
            CIDPDBInstall *obj = new CIDPDBInstall() ;
            int ret1 = obj->mymain(m_CSVFi leName);
            int ret2 = obj->IDPInstallCSVF ile(m_CSVFileNa me);
            // i can't return here.
            }

            IDPDBInstall.cp p//dll
            {
            int CIDPDBInstall:: mymain(const std::string m_CSVFileName)
            {
            ifstream inFile(m_CSVFil eName.c_str());
            inFile.close();
            return 0;
            }

            int CIDPDBInstall:: IDPInstallCSVFi le(const std::string m_CSVFileName);
            {
            ifstream inFile(m_CSVFil eName.c_str());
            inFile.close();
            return 0;
            }
            }


            As mentioned i can return to prg1.cpp after calling mymain but not after
            IDPInstall.


            regards,
            Venkat






            Comment

            • David Harmon

              #7
              Re: File Open and Close

              On Tue, 13 Jan 2004 21:33:50 +0530 in comp.lang.c++, "Venkat"
              <venkat_kp@yaho o.com> was alleged to have written:[color=blue]
              >I am getting problem like once i open and then read from the file and then
              >close it, the application is crashing.[/color]

              Chances are there is some problem elsewhere, such as a bad pointer write
              causing some kind of corruption, that only comes to light when close()
              tries to release memory or resources. See if you can create a very
              small example of your code that exhibits the problem, and if you can
              then post the code here.

              close() does not destruct the ifstream object, but it should release all
              resources at the system level associated with the open file. In your
              example, the ifstream will be destructed when it goes out of scope, when
              the function returns.

              Comment

              • Chris Theis

                #8
                Re: File Open and Close


                "Venkat" <venkat_kp@yaho o.com> wrote in message
                news:1074018895 .504673@sj-nntpcache-5...
                [SNIP][color=blue]
                > file prg1.cpp
                > {
                > ...
                > ....
                > CIDPDBInstall *obj = new CIDPDBInstall() ;
                > int ret1 = obj->mymain(m_CSVFi leName);
                > int ret2 = obj->IDPInstallCSVF ile(m_CSVFileNa me);
                > // i can't return here.
                > }
                >
                > IDPDBInstall.cp p//dll
                > {
                > int CIDPDBInstall:: mymain(const std::string m_CSVFileName)[/color]

                Just for practical reasons (doesn't have anything to do with your problem)
                pass the string as a reference!
                [color=blue]
                > {
                > ifstream inFile(m_CSVFil eName.c_str());[/color]

                As a general guideline you should always check if opening the file was
                successful!
                [color=blue]
                > inFile.close();
                > return 0;
                > }
                >
                > int CIDPDBInstall:: IDPInstallCSVFi le(const std::string m_CSVFileName);[/color]

                Here you must drop the semicolon! Probably that was a copy & paste mistake.
                [color=blue]
                > {
                > ifstream inFile(m_CSVFil eName.c_str());
                > inFile.close();
                > return 0;
                > }
                > }[/color]

                And the last curly brackets are superfluous.
                [color=blue]
                >
                >
                > As mentioned i can return to prg1.cpp after calling mymain but not after
                > IDPInstall.
                >[/color]

                Despite the things I mentioned the code looks okay. I'd recommend to step
                through IDPInstall with a debugger step by step so you see which function
                call causes the actual problems.

                Regards
                Chris


                Comment

                • Chris Theis

                  #9
                  Re: File Open and Close


                  "Venkat" <venkat_kp@yaho o.com> wrote in message
                  news:1074016072 .836673@sj-nntpcache-3...[color=blue]
                  > Evan,
                  >
                  > "Evan Carew" <tempcarew@pobo x.com> wrote in message
                  > news:10087q8pms liv73@corp.supe rnews.com...[color=green]
                  > > Vencat,[/color]
                  >[color=green]
                  > > Insofar as it goes, your code looks good. For instance, the following
                  > > program compiles and runs fine for me:
                  > >
                  > >
                  > > #include <fstream>
                  > >
                  > > int main(){
                  > > ifstream inFile("file1.t xt");
                  > > if (!inFile.good() ) cerr << "Problem opening file1.txt!" << endl;
                  > > inFile.close();
                  > > cout << "Success opening file1.txt!" << endl;
                  > > return 0;
                  > > }[/color]
                  >
                  > The above code turns to me also. Infact the subsequent open and close of
                  > same file runs fine, but i am not able to get a return value from the
                  > function and the app is crashing,
                  > i guess we are not releasing or calling the destructor properly and it
                  > could be reason of the application is crashing.
                  > I want to know inFile.close() would call the neccessary destructor to
                  > release the reasource? or am i missing something?
                  >[/color]

                  I guess it's necessary to clarify some things here. Calling the close method
                  will not result in the call of the destructor but it will/should release the
                  resource. If the ifstream object goes out of scope the dtor is called
                  automatically and will also release the allocated resources. However, I
                  think this is not really the point of your problem. Thus I'd suggest to step
                  through the code with a debugger to find the exact point of the crash.

                  HTH
                  Chris


                  Comment

                  • Rolf Magnus

                    #10
                    Re: File Open and Close

                    Venkat wrote:
                    [color=blue][color=green]
                    > > Insofar as it goes, your code looks good. For instance, the
                    > > following program compiles and runs fine for me:
                    > >
                    > >
                    > > #include <fstream>
                    > >
                    > > int main(){
                    > > ifstream inFile("file1.t xt");
                    > > if (!inFile.good() ) cerr << "Problem opening file1.txt!" <<
                    > > endl; inFile.close();
                    > > cout << "Success opening file1.txt!" << endl;
                    > > return 0;
                    > > }[/color]
                    >
                    > The above code turns to me also. Infact the subsequent open and
                    > close of same file runs fine, but i am not able to get a return
                    > value from the function and the app is crashing, i guess we are
                    > not releasing or calling the destructor properly and it could be
                    > reason of the application is crashing.
                    > I want to know inFile.close() would call the neccessary destructor
                    > to release the reasource?[/color]

                    No. It's the other way round. The destructor is only called on
                    destruction of the object (hence the name). But the destructor will
                    close the stream automatically if it's not yet closed, so you can
                    actually leave out the close() call. However, that is not related to
                    your problem.
                    [color=blue]
                    > or am i missing something?[/color]

                    You should make a minimal, but complete program (i.e. including main()
                    and being fully compilable) that still shows the problem. If you don't
                    find the error while doing that, post the result here.

                    Comment

                    • Venkat

                      #11
                      Can some one answer this!!!


                      "Venkat" <venkat_kp@yaho o.com> wrote in message
                      news:1074018895 .504673@sj-nntpcache-5...[color=blue]
                      > Chris,
                      >
                      > "Chris Theis" <Christian.Thei s@nospam.cern.c h> wrote in message
                      > news:bu19uc$36r $1@sunnews.cern .ch...[color=green]
                      > >
                      > > [SNIP]
                      > >[/color]
                      > Venkat Wrote:[color=green][color=darkred]
                      > > > > I am getting problem like once i open and then read from the[/color][/color][/color]
                      file[color=blue]
                      > and[color=green]
                      > > then[color=darkred]
                      > > > > close it, the application is crashing.
                      > > > >[/color]
                      > >
                      > > To the OP: Show us more of your actual code as the problem seems to[/color][/color]
                      be[color=blue][color=green]
                      > > located somewhere else.[/color]
                      >
                      > Here is the code.
                      >
                      > file prg1.cpp
                      > {
                      > ...
                      > ....
                      > CIDPDBInstall *obj = new CIDPDBInstall() ;
                      > int ret1 = obj->mymain(m_CSVFi leName);
                      > int ret2 = obj->IDPInstallCSVF ile(m_CSVFileNa me);
                      > // i can't return here.
                      > }
                      >
                      > IDPDBInstall.cp p//dll
                      > {
                      > int CIDPDBInstall:: mymain(const std::string m_CSVFileName)
                      > {
                      > ifstream inFile(m_CSVFil eName.c_str());
                      > inFile.close();
                      > return 0;
                      > }
                      >
                      > int CIDPDBInstall:: IDPInstallCSVFi le(const std::string m_CSVFileName);
                      > {
                      > ifstream inFile(m_CSVFil eName.c_str());
                      > inFile.close();
                      > return 0;
                      > }
                      > }
                      >
                      >
                      > As mentioned i can return to prg1.cpp after calling mymain but not[/color]
                      after[color=blue]
                      > IDPInstall.
                      >
                      >
                      > regards,
                      > Venkat
                      >
                      >
                      >
                      >
                      >
                      >[/color]



                      Comment

                      Working...