Connecting Tech Pros Worldwide Help | Site Map

File Open and Close

  #1  
Old July 22nd, 2005, 06:11 AM
Venkat
Guest
 
Posts: n/a
Hi,

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

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

ifStream inFile("file1.txt");
.......
.......
.......
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






  #2  
Old July 22nd, 2005, 06:11 AM
Thomas Matthews
Guest
 
Posts: n/a

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.txt");
> ......
> ......
> ......
> 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:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

  #3  
Old July 22nd, 2005, 06:11 AM
Evan Carew
Guest
 
Posts: n/a

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.txt");
> ......
> ......
> ......
> 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.txt");
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

  #4  
Old July 22nd, 2005, 06:11 AM
Venkat
Guest
 
Posts: n/a

re: File Open and Close


Evan,

"Evan Carew" <tempcarew@pobox.com> wrote in message
news:10087q8pmsliv73@corp.supernews.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.txt");
> 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








  #5  
Old July 22nd, 2005, 06:11 AM
Chris Theis
Guest
 
Posts: n/a

re: File Open and Close



"Evan Carew" <tempcarew@pobox.com> wrote in message
news:10087q8pmsliv73@corp.supernews.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.txt");
> 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


  #6  
Old July 22nd, 2005, 06:11 AM
Venkat
Guest
 
Posts: n/a

re: File Open and Close


Chris,

"Chris Theis" <Christian.Theis@nospam.cern.ch> 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_CSVFileName);
int ret2 = obj->IDPInstallCSVFile(m_CSVFileName);
// i can't return here.
}

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

int CIDPDBInstall::IDPInstallCSVFile(const std::string m_CSVFileName);
{
ifstream inFile(m_CSVFileName.c_str());
inFile.close();
return 0;
}
}


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


regards,
Venkat






  #7  
Old July 22nd, 2005, 06:12 AM
David Harmon
Guest
 
Posts: n/a

re: File Open and Close


On Tue, 13 Jan 2004 21:33:50 +0530 in comp.lang.c++, "Venkat"
<venkat_kp@yahoo.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.

  #8  
Old July 22nd, 2005, 06:13 AM
Chris Theis
Guest
 
Posts: n/a

re: File Open and Close



"Venkat" <venkat_kp@yahoo.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_CSVFileName);
> int ret2 = obj->IDPInstallCSVFile(m_CSVFileName);
> // i can't return here.
> }
>
> IDPDBInstall.cpp//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_CSVFileName.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::IDPInstallCSVFile(const std::string m_CSVFileName);[/color]

Here you must drop the semicolon! Probably that was a copy & paste mistake.
[color=blue]
> {
> ifstream inFile(m_CSVFileName.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


  #9  
Old July 22nd, 2005, 06:13 AM
Chris Theis
Guest
 
Posts: n/a

re: File Open and Close



"Venkat" <venkat_kp@yahoo.com> wrote in message
news:1074016072.836673@sj-nntpcache-3...[color=blue]
> Evan,
>
> "Evan Carew" <tempcarew@pobox.com> wrote in message
> news:10087q8pmsliv73@corp.supernews.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.txt");
> > 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


  #10  
Old July 22nd, 2005, 06:13 AM
Rolf Magnus
Guest
 
Posts: n/a

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.txt");
> > 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.

  #11  
Old July 22nd, 2005, 06:19 AM
Venkat
Guest
 
Posts: n/a

re: File Open and Close



"Venkat" <venkat_kp@yahoo.com> wrote in message
news:1074018895.504673@sj-nntpcache-5...[color=blue]
> Chris,
>
> "Chris Theis" <Christian.Theis@nospam.cern.ch> 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_CSVFileName);
> int ret2 = obj->IDPInstallCSVFile(m_CSVFileName);
> // i can't return here.
> }
>
> IDPDBInstall.cpp//dll
> {
> int CIDPDBInstall::mymain(const std::string m_CSVFileName)
> {
> ifstream inFile(m_CSVFileName.c_str());
> inFile.close();
> return 0;
> }
>
> int CIDPDBInstall::IDPInstallCSVFile(const std::string m_CSVFileName);
> {
> ifstream inFile(m_CSVFileName.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]



Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a vb script or batch file to open/close an ACCESS DB in a non-fixed location foltac answers 0 June 18th, 2009 03:24 PM
Detecting file open and close EliteBadger@gmail.com answers 7 September 27th, 2006 02:35 PM
File Open and Save dialog poping up while opening PDF Satya answers 4 January 23rd, 2006 06:05 PM
RE : is file open? Giraud, Sebastien answers 0 July 18th, 2005 07:44 AM