Đang chuẩn bị liên kết để tải về tài liệu:
thinking in c 2nd ed volume 2 rev 20 - phần 3
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Nếu bạn muốn an toàn hoàn toàn, bạn phải ngăn chặn người dùng truy cập trực tiếp con trỏ FILE. Một số phiên bản của tập tin bình thường tất cả các chức năng I / O phải hiển thị như là các thành viên lớp để tất cả mọi thứ bạn có thể làm với phương pháp tiếp cận C có sẵn trong C + + class | 105z516 If you want complete safety you must prevent the user from directly accessing the FILE pointer. Some version of all the normal file I O functions must show up as class members so that everything you can do with the C approach is available in the C class Comment C04 Fullwrap.h Completely hidden file IO ifndef FULLWRAP_H define FULLWRAP_H class File std FILE f std FILE F Produces checked pointer to f public File Create object but don t open file File const char path const char mode r File int open const char path const char mode r int reopen const char path const char mode int getc int ungetc int c int putc int c int puts const char s char gets char s int n int printf const char format . size_t read void ptr size_t size size_t n size_t write const void ptr size_t size size_t n int eof int close int flush int seek long offset int whence int getpos fpos_t pos int setpos const fpos_t pos long tell void rewind void setbuf char buf int setvbuf char buf int type size_t sz int error void clearErr endif FULLWRAP_H This class contains almost all the file I O functions from cstdio . vfprintf is missing it is used to implement the printf member function. Comment File has the same constructor as in the previous example and it also has a default constructor. The default constructor is important if you want to create an array of File objects or use a File object as a member of another class in which the initialization doesn t happen in the constructor but some time after the enclosing object is created. Comment The default constructor sets the private FILE pointer f to zero. But now before any reference to f its value must be checked to ensure it isn t zero. This is accomplished with F which is private ions. We don t want to give the user 38 Comment because it is intended to be used only by other member func direct access to the underlying FILE structure in this class. 106z516 This approach is not a terrible solution by any means. It s quite functional and you could .