INTERVIEW QUESTIONS
C++
INPUT AND OUTPUT OPERATIONS IN C++
DETAILS
Question: Why do we need to close a file. Eg. ifstream ifile; ifile.open("temp.dat") ..... ... ifile.close(); What happens if we do not close() a file?
Answer: Normally file is close automatically if an application terminates normally, however if the application terminates abruptly like with a call to abort() or when application crashes, the file is not closed properly. Consequences are:<br>1. Data might be lost which was still in buffer and not flushed to file.<br>2. In worst case, the file my be destroyed.<br>3. intermittent problems in the program.<br>4. there is a system limit on how many files can be open at any one point, so not closing the file stream may increase this number (not 100% sure here). and you know the impact then.
|
Question:
Why do we need to close a file. Eg. ifstream ifile; ifile.open("temp.dat") ..... ... ifile.close(); What happens if we do not close() a file?
Answer:
Normally file is close automatically if an application terminates normally, however if the application terminates abruptly like with a call to abort() or when application crashes, the file is not closed properly. Consequences are:<br>1. Data might be lost which was still in buffer and not flushed to file.<br>2. In worst case, the file my be destroyed.<br>3. intermittent problems in the program.<br>4. there is a system limit on how many files can be open at any one point, so not closing the file stream may increase this number (not 100% sure here). and you know the impact then. Source: CoolInterview.com
Answered by: Harry | Date: 6/15/2008
| Contact Harry
hi,<br> nothing will happen if u are working in xp os, but in case of linux it will show the file size as 0 bytes, it should required ostream.close Source: CoolInterview.com
Answered by: giritharan | Date: 6/16/2008
| Contact giritharan
Every operating system maintains few structures for any file system resources. A File is of no exception. By opening up a file we are passing our request to underlying OS, which in turn sets up necessary structures to keep track of several low details required for file handling. By closing a file we inform the underlying OS that we are done with the file and OS can release the structures it created for file manipulation.<br><br>If we don't close the file, OS resources will remain open, which may lead to memory leak and so a lot of problems associated with memory leak. Although most operating system makes sure that all the OS resources allocated to a process is released when the process terminates, it may cause problems while process is running. Source: CoolInterview.com
Answered by: Vinit Kumar | Date: 6/16/2008
| Contact Vinit Kumar
It is very simple,when u open a file from a hard disk that file will loaded into the RAM there it occupies some memory.After using that file if we dont close that, the memory occupied in the buffer will not be released n that memory cannot be used by other processes / applications.It does give any error or exception when we open a file for reading.But when we dont close a file when it is opened for writing the contents are not saved permanently.they will be lost in the RAM itself. Further clarifications contact me on my mail id. Source: CoolInterview.com
Answered by: K V Kiran Kumar | Date: 9/7/2008
| Contact K V Kiran Kumar
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|