File InputOutput in C ++: Read File

zhaozj2021-02-11  197

File input / output (2) in C : ilia yordanov, loobian@cpp-home.com

Read file

You have seen how you should write a file. Now, when we have got a cpp-home.txt file, we will read it and print the content on the screen.

First of all, I have to point out that there are many ways to read files. I will introduce you to all methods (I know). At this moment, I will show you the best way (I think).

As you are already familiar - I will first give a program code, then I will explain it in detail:

#include

Void main () // Program starts here

{

IFStream OpenFile ("cpp-home.txt");

CHAR CH;

While (! OpenFile.eof ())

{

OpenFile.get (CH);

Cout << CH;

}

Openfile.close ();

}

You have to understand the meaning of the first line, and the remaining part will be explained by me.

IFStream OpenFile ("cpp-home.txt") - I guess it will be familiar with now, you will be familiar! IFStream represents "Input File Stream". In the previous program, it is OFStream, its meaning is "Output File Stream". The program in the previous section is a write operation of the file, which is why it is expressed in "Output". The program of this section is to read a file, which is why it is expressed in "INPUT". The remaining code left in this line should be familiar: OpenFile is an object of the IFStream class, which will associate an input file stream; and use the quotation number, it is the name of the file to open.

Please note: There is no test for the file to be opened here! I will point out how to test it later.

CHAR CH; - Declare an array of type char. Just a little to remind you: ARRAYS can only store an ASCII character.

While (! openfile.eof ()) - If the end of the file has been reached, the EOF () function returns a non-zero value. So this loop we designed will continue until our file operation arrives at the end of the file. This way we can traverse the entire file in order to read it.

OpenFile.get (CH); - OpenFile is an object of class ifstream. This class declares a member function called GET (). As long as we own the object, we naturally call this function. The get () function reads a character from the corresponding stream file and returns it to the variable. In this example, the GET () function is only one parameter - a variable used to store the read character. Therefore, the program will read a character from the OpenFile stream and store the variable CH from the OpenFile stream.

Note: If you call this function again, it will read the next character, not the original one! Will you understand why you will be like this.

This is why we have to repeatedly loop until the read operation arrives at the end of the file. Every time you cycle, we will read a character and save it in the CH.

COUT << ch; - Displays the CH variable, which saves the read character.

File.close (); - We open a streaming file, you need to close it. Use the close () function to turn it off, which is the same as the previous section! Note: Once you turn off a file, you can't access it before you reopen it.

Come it! I hope you can understand my explanation. When you compile and run this program, it should output:

"Hello World, from www.cpp-home.com and lotion!"

转载请注明原文地址:https://www.9cbs.com/read-5172.html

New Post(0)