文件打开和关闭 [英] File Open and Close

查看:69
本文介绍了文件打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我使用ifstream打开一个文件读取然后关闭它。


示例代码

-------------------


ifStream inFile(" file1.txt");

.......

.......

.......

inFile .Close();

只需通过发出语句inFile.Close()将关闭所有流

对象或我们需要任何进一步的语句才能释放文件

完全(比如发布与文件相关的所有内容,以便它可用

进一步打开/读取类似inFile.Unlock(),inFile.Clear())


我遇到的问题就像我打开然后从文件中读取然后

关闭它,应用程序崩溃了。


问候,

Venkat

解决方案

Venkat写道:



我正在使用ifstream打开一个文件读取它关闭它。

示例代码
-------------------

ifStream inFile(" file1.txt");
......
......
......
inFile.Close();

只需发出语句inFile.Close()将关闭所有流对象或我们需要任何进一步的语句才能完全释放文件(比如释放与文件相关的所有内容)所以它可用于进一步打开/读取像inFile.Unlock(),inFile.Clear())

我遇到的问题就像我打开然后从文件中读取然后
关闭它,应用程序崩溃。

问候,
Venkat




1. C ++语言是区分大小写。 ifstream方法是关闭

不是关闭。


2.向我们展示开启和关闭之间的代码,实际行为和

预期的行为。


-

Thomas Matthews


C ++新闻组欢迎消息:
http://www.slack.net/~ shiva / welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: 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

其他网站:
http:/ /www.josuttis.com - C ++ STL图书馆书籍


Vencat,

Venkat写道:



我正在使用ifstream打开一个文件读取然后关闭它。

示例代码
----- --------------

ifStream inFile(" file1.txt");
......
... ......
......
inFile.Close();
就此而言,你的代码看起来不错。例如,以下

程序编译并运行正常:

#include< fstream>


int main( ){

ifstream inFile(" file1.txt");

if(!inFile.good())cerr<< 问题打开file1.txt! << endl;

inFile.close();

cout<< 成功打开file1.txt! << endl;

返回0;

}


只需发出语句inFile.Close()即可关闭所有流
对象或我们需要任何进一步的声明才能完全释放文件(比如发布与文件相关的所有内容以便它可用于进一步打开/读取像inFile这样的内容.Unlock(),inFile.Clear())

我遇到的问题就像我打开然后从文件中读取然后关闭它,应用程序崩溃了。


听起来你的代码遇到了另一个问题。你有没有尝试用ddd或其他工具查看核心文件?

Evan Carew


< blockquote> Evan,


" Evan Carew" < TE ******* @ pobox.com>在消息中写道

news:10 ************* @ corp.supernews.com ...

Vencat,
就此而言,你的代码看起来很棒。例如,以下
程序编译并运行正常:

#include< fstream>

int main(){
ifstream inFile (" file1.txt");
if(!inFile.good())cerr<< 问题打开file1.txt! << endl;
inFile.close();
cout<< 成功打开file1.txt! << endl;
返回0;
}




以上代码也转向我。事实上后续打开和关闭

相同的文件运行正常,但我无法从

函数获得返回值,并且应用程序崩溃了,

i猜我们没有正确释放或调用析构函数,它可能是应用程序崩溃的原因。
我想知道inFile.close( )会调用必要的析构函数来释放资源吗?或者我错过了什么?

问候,

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


解决方案

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


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


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



这篇关于文件打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆