本文介绍了文件打开和关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用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图书馆书籍 Evan, " Evan Carew" < TE ******* @ pobox.com>在消息中写道 news:10 ************* @ corp.supernews.com ... 以上代码也转向我。事实上后续打开和关闭 相同的文件运行正常,但我无法从 函数获得返回值,并且应用程序崩溃了, 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 streamobject or we need to any further statements in order to release the filecompletely(like releasing everything related to the file so it is availablefor further opening/reading something like inFile.Unlock(), inFile.Clear())I am getting problem like once i open and then read from the file and thenclose 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 andthe expected behavior.--Thomas MatthewsC++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txtC++ Faq: http://www.parashift.com/c++-faq-liteC Faq: http://www.eskimo.com/~scs/c-faq/top.htmlalt.comp.lang.learn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.htmlOther sites: http://www.josuttis.com -- C++ STL Library bookIt sounds like your code is suffering from another problem. Have youtried looking at the core file with ddd or some other tool?Evan CarewThe above code turns to me also. Infact the subsequent open and close ofsame file runs fine, but i am not able to get a return value from thefunction and the app is crashing,i guess we are not releasing or calling the destructor properly and itcould be reason of the application is crashing.I want to know inFile.close() would call the neccessary destructor torelease the reasource? or am i missing something?regards,Venkat 这篇关于文件打开和关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 14:59
查看更多