473,385 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

File Open and Close

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


Jul 22 '05 #1
10 3116
Venkat wrote:
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

Jul 22 '05 #2
Vencat,
Venkat wrote:
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

Jul 22 '05 #3
Evan,

"Evan Carew" <te*******@pobox.com> wrote in message
news:10*************@corp.supernews.com...
Vencat, 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


Jul 22 '05 #4

"Evan Carew" <te*******@pobox.com> wrote in message
news:10*************@corp.supernews.com...
[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
Jul 22 '05 #5
Chris,

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bu**********@sunnews.cern.ch...

[SNIP]
Venkat Wrote:
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.


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


Jul 22 '05 #6
On Tue, 13 Jan 2004 21:33:50 +0530 in comp.lang.c++, "Venkat"
<ve*******@yahoo.com> was alleged to have written:
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.

Jul 22 '05 #7

"Venkat" <ve*******@yahoo.com> wrote in message
news:1074018895.504673@sj-nntpcache-5...
[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
Jul 22 '05 #8

"Venkat" <ve*******@yahoo.com> wrote in message
news:1074016072.836673@sj-nntpcache-3...
Evan,

"Evan Carew" <te*******@pobox.com> wrote in message
news:10*************@corp.supernews.com...
> Vencat,

> 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?


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
Jul 22 '05 #9
Venkat wrote:
> 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?


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.

Jul 22 '05 #10

"Venkat" <ve*******@yahoo.com> wrote in message
news:1074018895.504673@sj-nntpcache-5...
Chris,

"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bu**********@sunnews.cern.ch...
>
> [SNIP]
> Venkat Wrote:
> > > 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.


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



Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Bryan | last post by:
i'm curious to know how others handle the closing of files. i seem to always end up with this pattern even though i rarely see others do it. f1 = file('file1') try: # process f1 finally:...
16
by: Chuck Amadi | last post by:
Sorry to bovver you again (again) here's script. I still can't see why the get_payload() doesn't produce the plain text message body of an emails in the testwwws users mailbox. As you can see I...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
11
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
24
by: Kelly | last post by:
Hey all - I need a little more help. I don't quite know why my text file or form isn't closing. Short version - this program takes data entered into a textbox, user clicks Save button, Save...
18
by: panig | last post by:
how the program knows when a file is finsihed, mmm?
2
by: raan | last post by:
Please see the following program. My intention is to open the file (create it if it does not exist), or if the file exists already it should be truncated (the entire contents is thrown away) and...
5
by: Steven D'Aprano | last post by:
After reading an earlier thread about opening and closing lots of files, I thought I'd do a little experiment. Suppose you have a whole lot of files, and you need to open each one, append a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.