Answer : File handling is a process that allows a C++ program to store data permanently in a file and retrieve it later when needed. Unlike RAM where data is stored temporarily, data stored in files remain available even after a program terminates or the computer is switched off
2. Why do we need Data File Handling?
Answer :Data File Handling is needed because without files:
Data is lost after program execution.
Records cannot be reused.
3. What are the types of data files?
Answer :
Text files
Binary files
4. What are text files?
Answer : Data is stored in the form of characters
5. What are binary files?
Answer : Data is stored in the form of bytes (0 and 1).
6. Write the difference between text file and binary files?
Answer :
Text file
Binary file
Text file data is stored in the form of characters
Binary file data is stored in the form of bytes (0 and 1).
Human-readable
Not human readable
7. What are the file stream classes?
Answer :
ifstream
ofstream
fstream
8. Which header file is used for file handling?
Answer :
<fstream.h>
9. What are the 2 methods of opening a file?
Answer :
1.using constructor
2. using open function
10. What are the different file opening modes?
Answer :
ios::in
ios::out
ios::ate
ios::binary
ios::trunc
11. How do we detect end of a file?
Answer : When the end of a file is reached , the stream returns a special value called EOF (End of File)
Methods to detect end of file
1.fin.eof( ) : Returns true if end of file is reached
2.fin.fail( ) : Returns true if any input operation fails (including EOF).
12. Name some common functions used for file handling.
Answer :
1.get(char & ) – reads one character from file
2.put(char) – writes one character into file
3.getline( ) – reads a line from file (upto newline)
4.read(char*,n) – reads n bytes from binary file
5.write(char*,n) – writes n bytes into binary file
13. What do you mean by random access in files?
Answer :
Random access means accessing any record directly without reading all previous records. We can move the read/write pointer to any position in a file using the following functions: