C + + Read file operation Peek, >> and get

Source: Internet
Author: User

Pre-Knowledge:
FStream provides three classes for implementing C + + operations on files. (file creation, reading, and writing).
Ifstream-read from an existing file
Ofstream-Write content to a file
FStream--Open file for read and write

File Open mode:
Ios::in Read
Ios::out Write
Ios::app start at the end of the file
Ios::binary binary mode
Ios::nocreate when a file is opened, the file is not created if the file does not exist.
Ios::noreplace when you open a file, if the file does not exist, create the file
Ios::trunc open a file, and then empty the content
Ios::ate when you open a file, move the location to the end of the file

The use of the file pointer position in C + +:
Ios::beg file Header
Ios::end file Footer
Ios::cur Current Location
Example:
FILE.SEEKG (0,ios::beg); To position the file pointer at the beginning of the file
FILE.SEEKG (0,ios::end); To position the file pointer to the end of the file
FILE.SEEKG (10,ios::cur); Let the file pointer move 10 bytes from the current position to the end of the file
FILE.SEEKG ( -10,ios::cur); Let the file pointer move 10 bytes from the current position to the beginning of the file
FILE.SEEKG (10,ios::beg); Position the file pointer to a location 10 bytes from the beginning of the file

Common methods of error determination:
Good () If the file opens successfully
Bad () An error occurred while opening the file
EOF () reaches end of file

Above excerpt from: http://blog.csdn.net/wangshihui512/article/details/8921919

Places to be aware of:
1. The,>> operator automatically ignores spaces and wraps when the file is read. For example, the input file contents are as follows:

8#0 1 11 015 59 60 63 132 169 176 19217 4 13 30 49 56 60 92 114 132 162 165 169 176 18221 2 30 50 57 63 92 94 100 114 126 162 165 176 182 192#1 1 2 01 11 31 35 69 114 162 165 1826 18 67 107 110 158 162 183.
Note that there are no spaces after each line, and the last line is a blank line.

There are three of the following code:
Code Listing 1:

FStream Infile;infile.open (FileName, ios::in); int inum;infile>>inum;cout<< (char) infile.peek () << Endl
Guess what the output is? A. Line break B. #
Code Listing 2:

FStream Infile;infile.open (FileName, ios::in); int Inum;infile>>inum;char c;infile>>c;cout<<c< <endl;
Guess what the output is? A. Line break B. #
Code Listing 3:

FStream Infile;infile.open (FileName, ios::in); int Inum;infile>>inum;char C;infile.get (c);cout<<c< <endl;
guess what the output is? A. Line break B. #

I believe most people may make the same mistake as I do, thinking that it is output # or confused.
in fact, code 1 and Code 3 output is a newline character, code 2 output is #.
Accordingly, we can draw the following conclusions:
(1) The >> operator ignores the preceding whitespace and line breaks, but does not go beyond the following line breaks and whitespace characters
(2) The Get () method does not skip any symbols
(3) Peek () method pre-reads the next character (no matter what the symbol)
2, using peek () to determine the completion of reading
The same is the input file in 1, if the code reads as follows:

FStream Infile;infile.open (FileName, Ios::in), while (!infile.eof ()) {    //related read operation}
The last line of blank lines also enters the loop, causing a read error.
The peek () method can therefore be used to avoid this problem:
FStream Infile;infile.open (FileName, Ios::in), while (!infile.eof () &&infile.peek ()!=eof) {    //related read operation}

reprint Please specify: Kangrui tribe ? C + + Read file operation Peek, >> and get

C + + Read file operation Peek, >> and get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.