Thinking In C ++ Volume 2 Chinese version

zhaozj2021-02-16  59

Let us pay attention to the spirit of Bruceeckel

IO stream

You Can do Much More With the General I / O Problem Than Just Take Standard I / O and Turn It Into a class.

If you can generate the same container - standard IO, file, or even memory blocks, so that you only have to remember an interface, isn't this not very good? This is the idea of ​​IO back. They are more simple, safe, and sometimes more efficient than the diversity of standard C stdio libraries.

IO streaming is usually the first part of C new programmers to learn to use the C class library. This chapter will discuss how IO flow is improved on C stdio components, how to check the behavior of files, string streams, and standard control flow.

Why is IO?

You may want to know what defects in the old performance of a good C library. Why don't you pack the C library and use it again? Sometimes this is a good solution. For example, assume that you want to confirm that the file whose Stdio File pointer is secure, and it does not need to rely on programmers to call the Close () function is appropriate to close it. The following procedure is one this attempt:

//: c04: fileclass.h

// stdio files wrapped.

#ifndef fileclass_h

#define fileclass_h

#include

#include

Class fileclass {

Std :: file * f;

PUBLIC:

Struct FileClasSerror: std :: runtime_error {

FileClasserror (const char * msg)

: std :: runtime_error (msg) {}

}

FileClass (const char * fname, const char * mode = "r");

~ FileClass ();

Std :: file * fp ();

}

#ENDIF / / FileClass_H / / /: ~

When you perform a file IO operation in C, you use a bare pointer to the File structure. However, this class wraps a layer on the pointer, using constructor and destructive function to ensure that it is correctly initialized and cleared. The parameter of the second constructor is the open mode of the file, default is 'r' (Read).

In order to get the value of the pointer in the file IO function, you have to use the fp () to access the function. Here is a member function definition:

//: c04: fileclass.cpp {o}

// FileClass IMPLEMENTATION.

#include "fileclass.h"

#include

#include

Using namespace std;

FileClass :: FileClass (const char * fname, const char * MODE) {

IF ((f = fopen (fopen (fopen (fopen)) == 0)

Throw FileClasserror ("Error Opening File");

}

FileClass :: ~ FileClass () {fclose (f);

File * fileclass :: fp () {returnif;} ///: ~

As you usually do, constructor call fopen (), but it also guarantees that the result is not 0, and 0 is indicating that the open file failed. If the file cannot be opened by hope, an exception will thrown.

The destructor is turned off, and the access function returns F. This is a simple example of using FileClass: //: c04: fileclasstest.cpp

// {l} fileclass

#include

#include

#include "fileclass.h"

Using namespace std;

Int main () {

Try {

FileClass F ("FileClasstest.cpp");

Const int bsize = 100;

Char buf [BSIZE];

While (FGETS (BUF, BSIZE, F.FP ())))

FPUTS (BUF, STDOUT);

} catch (fileclass :: fileclasserror & e) {

Cout << E.WHAT () << endl;

Return EXIT_FAILURURE;

}

Return EXIT_SUCCESS;

} // file automaticly closed by Destructor

/ //: ~

You created a FileClass object and use it by calling FP () with the usual C file I / O call mode. When you handle it, I forget her. The end of the field is turned off at the end of the scope.

If you want to publish this document in a variety of media, please do not modify this document. As a learning and communication purposes, this document is free. If you use this document directly for commercial purposes, please contact me before use, you can use it after you agree. Otherwise, everything is at your own risk! The English original text of this document can be found on the Bruceeckel's website (www.bruceekel.com). Let us pay great respect for Bruceeckel's open source spirit.

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

New Post(0)