The ignore( ) Function. McGraw-Hill The Complete Reference Fourth Edition
Annonce
Annonce
C h a p t e r 2 1 : C + + F i l e I / O
555
When this program is used to display itself, the first screen looks like this:
2F 2A 20 44 69 73 70 6C 61 79 20 63 6F 6E 74 65 /* Display conte
6E 74 73 20 6F 66 20 73 70 65 63 69 66 69 65 64 nts of specified
20 66 69 6C 65 D A 20 20 20 69 6E 20 62 6F 74 file.. in bot
68 20 41 53 43 49 49 20 61 6E 64 20 69 6E 20 68 h ASCII and in h
65 78 2E D A 2A 2F D A 23 69 6E 63 6C 75 64 ex...*/..#includ
65 20 3C 69 6F 73 74 72 65 61 6D 3E D A 23 69 e <iostream>..#i
6E 63 6C 75 64 65 20 3C 66 73 74 72 65 61 6D 3E nclude <fstream>
D A 23 69 6E 63 6C 75 64 65 20 3C 63 63 74 79 ..#include <ccty
70 65 3E D A 23 69 6E 63 6C 75 64 65 20 3C 69 pe>..#include <i
6F 6D 61 6E 69 70 3E D A 75 73 69 6E 67 20 6E omanip>..using n
61 6D 65 73 70 61 63 65 20 73 74 64 3B D A D amespace std;...
A 69 6E 74 20 6D 61 69 6E 28 69 6E 74 20 61 72 .int main(int ar
67 63 2C 20 63 68 61 72 20 2A 61 72 67 76 5B 5D gc, char *argv[]
29 D A 7B D A 20 20 69 66 28 61 72 67 63 21 )..{.. if(argc!
3D 32 29 20 7B D A 20 20 20 20 63 6F 75 74 20 =2) {.. cout
3C 3C 20 22 55 73 61 67 65 3A 20 44 69 73 70 6C << "Usage: Displ
Press ENTER to continue:
The ignore( ) Function
You can use the ignore( ) member function to read and discard characters from the input stream. It has this prototype: istream &ignore(streamsize num=1, int_type delim=EOF);
It reads and discards characters until either num characters have been ignored (1 by default) or the character specified by delim is encountered (EOF by default). If the delimiting character is encountered, it is not removed from the input stream. Here,
int_type
is defined as some form of integer.
The next program reads a file called TEST. It ignores characters until either a space is encountered or 10 characters have been read. It then displays the rest of the file.
#include <iostream>
#include <fstream> using namespace std; int main()
{ ifstream in("test");
Download
Annonce