File inputoutput in C ++ (1): Your first program

zhaozj2021-02-11  179

File input / output in C (1)

Original: Ilia Yordanov, Loobian@cpp-home.com

Introduction

This tutorial will begin with C most basic file I / O (output / output). Since then, I will show you some techniques from a more in-depth aspect and analyze some useful functions.

You need to have a better understanding of C , otherwise this tutorial will be unfamiliar and useless.

Your first program

First of all, I will give a code, then explain one by line. Our first program will create a file and write some characters:

#include

Void main () // The program starts from here {

OFSTREAM Savefile ("CPP-Home.txt");

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

Savefile.close ();

Just this? That's right! This program will create a file called cpp-home.txt in the current run directory and write it to it "Hello World, from www.cpp-home.com and loke!".

The meaning of each line is given below:

#include - You need this file to use the C file input / output function. Note: Once this file is included, you no longer need (in order to use Cout / Cin) containing iostream.h because fstream.h has automatically contains it.

Declaring a number of classes in this header, including IFStream, OFSTREAM, and FSTREAM, they all inherit from ISTREAM and OSTREAM classes.

OFSTREAM Savefile ("CPP-Home.txt");

1) OFSTREAM is "Output File Stream". It will create a handle so that we will write files in the form of a file stream.

2) Savefile - This is the name of the file handle, of course, you can also switch any of the names you want.

3) ("cpp-home.txt"); - Open the file called cpp-home.txt. If the current directory runs in the program already exists, it will be replaced; if there is no existence, the program will create a file for you, you don't have to worry about it.

Now let us go deep into a little bit. First of all, I have to point out that ostream is a class. So OFStream Savefile; this statement will create an object of such a class; and we will pass the parameters transmitted in parentheses actually pass to constructor: here we will build the files we have to build The name is passed as the actual parameter to the constructor of this class. Of course, we can also pass some other information, but I will explain it later.

Savefile << "Hello World, from www.cpp-home.com and lotion!"; - "<<" It is very kind? Yes, I want you to see it in cout <<. This is a predefined optic operator. Regardless of how to say, this line statement is done, writing the text above into the file. As mentioned earlier, Savefile is a file handle that is associated with an open stream file. Therefore, we only have to enter the handle, followed by "<<", and then write the text enclosed in a series of quotes, you can implement the writing of the file. If we want to write a variable of a variable rather than a quotation number, it is only likely to pass the variable to the handle object like usually using cout <<, like this: savefile << variablename;

Yes!

Savefile.close (); - Since we opened a stream file, then when we finished it, it must be closed. Savefile is an object of the OFStream class, and this class has a member function for turning off file, a Close () function. Therefore, we can turn off the file as long as we enter the file handle name, point number and close ().

Note: Once you turn off the file, you can't access it before you reopen it.

The above is the simplest program that can write files. It's really easy! However, as you are about to be seen in the later part of the tutorial, there are more things to learn!

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

New Post(0)