Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Data file handling
- File:-
- -A file is stream of bytes stored on some secondary storage device.
- -Files are of 2 types:
- Text file- A text file stores information in readable and printable form. Each line of text is terminated with an EOL (End of line) character.
- Binary File- A binary file conatins information in the same format iun which it is held in a memory.
- File stream:-
- -Stream is a general term to used to state flow of data.
- -ifstream: Used for read operations
- -ofstream: Used for write operations
- -fstream: Used for both read and write operations
- -fstream.h: This header file includes definations for the stream classes ifstream, ofstream and fstream. It also contains pre-defined set of operations for input output.
- -A file stored in secondary storage can be opened by 2 methods: 1-By constructor method-This will use default streamns for file input or output. This method is preffered when file is opened in input or output mode only, e.g- ofstream file("student.dat"); or ifstream file("student.dat");
- -By the open() member function of the stream. It is prefferd when file is opened in various modes i.e ios::in, ios::out, ios::append, ios::ate. E.g fstream.open("book.dat",ios::in|ios::out|ios::binary);
- File Modes:- (Read from book)
- -out mode
- -in mode
- -append mode
- -truncate mode
- -no create mode
- -no replace mode
- -binary mode
- File Functions:-
- -eof() function: This function dertermines the end of files by returning 'true'
- -close(): This functions termantes the connection between the file and stream associated e.g file.close();
- Text-File Functions:-
- -get(): It reads a single charcter from text file and stores it in a buffer. E.g file.get(ch);
- -put(): It writes a single character in text file. E.g file.put(ch);
- -getline(): It reads a line of text from text-file. E.g file.getline(ch,80); We can also use 'file>>ch' for reading and 'file<<ch' for writing but this operator does not accept white spaces.
Advertisement
Add Comment
Please, Sign In to add comment