Quick Revision in Text Files in C
Quick Revision in Text Files in C
Text file: A file that contains data in the form of ASCII characters. It is readable as well as
printable using notepad. Character translation takes place for eg. \n or endl is converted into a new
line.
File Modes:
1. ios::in – for reading from a file
2. ios::out – for writing in a file ( always starts fresh)
3. ios::app – for appending in a file
4. ios::ate – for appending by default and also writing any where in the file.
5. ios::in | ios::out – for reading and writing i.e for file modification
6. There is no explicit mode for a text file
1. char ch;
f1.get(ch);
A single character is read from the current position of get pointer in the file f1 into the
character ch.
2. char w[20];
f1>>w;
A word (multiple characters without spaces) is read from the current position of get
pointer in the file f1 into the array w.
3. char s[80];
f1.getline(s,80);
A line (multiple characters with spaces) is read from the current position of get pointer in
the file f1 into the array s.
4. char s[30];
f1<<s;
The array s (multiple characters with/without spaces) is written in the file f1 from the
current position of put pointer.
5. char ch;
f1.put(ch);
The character ch (single character) is written in the file f1 from the current position of put
pointer.
6. f1.seekg(10); or f1.seekg(10,ios::beg);
sets the get pointer on the 10th byte from the beginning of the file, so that reading can take
place from 10th byte onward.
7. f1.seekg(-15,ios::cur);
sets the get pointer on the 15th byte backward from the current position of the get pointer
in the file, so that reading can take place from that byte onward.
8. f1.tellg( );
returns the current position of the get pointer in the file f1.
eg. int pos;
pos=f1.tellg( );