File Open and Close

V

Venkat

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
 
T

Thomas Matthews

Venkat said:
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

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
 
E

Evan Carew

Vencat,

Hi,

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

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

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

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

Chris Theis

[SNIP]
Insofar as it goes, your code looks good. For instance, the following
program compiles and runs fine for me:

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.
#include <fstream>

int main(){
ifstream inFile("file1.txt");
if (!inFile.good()) cerr << "Problem opening file1.txt!" << endl;

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.
inFile.close();
cout << "Success opening file1.txt!" << endl;
return 0;
}
[SNIP]
I am getting problem like once i open and then read from the file and then
close it, the application is crashing.

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

[SNIP]

Regards
Chris
 
V

Venkat

Evan,

Evan Carew said:
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;
}

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
 
V

Venkat

Chris,

Chris Theis said:
[SNIP]
and

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

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
 
D

David Harmon

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

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

Chris Theis

[SNIP]
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)

Just for practical reasons (doesn't have anything to do with your problem)
pass the string as a reference!
{
ifstream inFile(m_CSVFileName.c_str());

As a general guideline you should always check if opening the file was
successful!
inFile.close();
return 0;
}

int CIDPDBInstall::IDPInstallCSVFile(const std::string m_CSVFileName);

Here you must drop the semicolon! Probably that was a copy & paste mistake.
{
ifstream inFile(m_CSVFileName.c_str());
inFile.close();
return 0;
}
}

And the last curly brackets are superfluous.
As mentioned i can return to prg1.cpp after calling mymain but not after
IDPInstall.

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
 
C

Chris Theis

Venkat said:
Evan,




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?

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
 
R

Rolf Magnus

Venkat said:
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?

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.
or am i missing something?

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

Venkat

Venkat said:
Chris,

Chris Theis said:
[SNIP]
Venkat said:
I am getting problem like once i open and then read from the
file
and

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

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
 

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
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top