C - Working With Files
C - Working With Files
in C++
Knowledge is power
Working With Files
Topics to be covered :
Introduction
Input stream and output streams
File stream Hierarchy
How to open a file
File pointers
File modes
Hands on Training
Questionnaire
Working with Files : INTRODUCTION *
A file can be of any type, binary file , txt file, cpp file etc.
Use : A file can be used to store input / output.
It is of two types :
1. Input Stream
2. Output Stream
File streams: INTRODUCTION **
1. Input Stream
2. Output Stream
Input Stream
Output
Input
InputStream
Stream
OutputScreen
Screen
Screen
Program
Keyboard
Input Stream
ios :: app append Open the file for appending data to end-of-file.
File handling functions / methods
a) open () :
Syntax :
File_stream_class stream_object ;
stream_object.open ("file name",[modes]);
b) Write(): This function is used to read data from primary/ main
memory and write it into secondary memory.
It is used to read data from memory variable and write it into secondary
memory.
c)Read():-This function read data from secondary memory and write it
into memory variable.
d) eof():-This function will return true(1) at end of file otherwise false(0).
e) close():-Use to close the file.
File stream classes **
Basic / default file modes
ofstream ios::out
ifstream ios::in
ios::in Open file for input operations . That is , for reading only.
ios::out Open file for output operations. That is , for writing only.
ios::binary Open file in binary mode.
ios::ate Go to the end of file on opening.
ios::app Append to the end of file
ios::trunc Delete contents of file, if it exists
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
Questionnaire : Working with files **
A. Theory Questions :
1. Explain file stream classes / hierarchy. (Board Theory) **
2. Explain input stream and output stream for file. **
3. Define input stream and output stream and explain their uses for
file operations. **
4. Define and explain file mode pointers.
5. Differentiate between :
• Input stream and output stream
• Read and write mode for file
• Ifstream and ofstream
• seekg() and seekp()
• tellg () and tellp()
Questionnaire : Working with files *
3.Define the following:
1. stream
2. Input stream
3. Output stream
4. ifstream
5. ofstream
6. fstream
7. seekg()
8. seekp()
9. tellg()
10. tellp()
11. opening a file in read mode
12. opening a file in write mode
END OF MODULE