Absolute C++ (4th Edition) part 54

Absolute C++ (4th Edition) part 54. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | Random Access to Files 537 include fstream using namespace std int main fstream rwStream stuff ios in ios out If you prefer you may use the following in place of the last two of the previous lines fstream rwStream stuff ios in ios out After this your program can read from the file stuff using the stream fstream and can also write to the file stuff using the same stream. There is no need to close and reopen the file when you change from reading to writing or from writing to reading. Moreover you have random access for reading and writing to any location in the file. However there are other complications. At least two complications arise when reading and writing with random access via an fstream 1 You normally work in bytes using the type char or arrays of char and need to handle type conversions on your own and 2 you typically need to position a pointer indicating where the read or write begins before each read or write. The constraints of finding a position and replacing one portion of a file with new data mean that most such random-access I O is done by reading or writing records in the form of structs or classes . One record or an integral number of records is read or written after each positioning of the pointer. Each fstream object has a member function named seekp that is used to position the put-pointer at the location where you wish to write put data. The function seekp takes a single argument which is the address of the first byte to be written next. The first byte in the file is numbered zero. For example to position the pointer in the file connected to the fstream rwStream at the 1000th byte the invocation would be as follows 1000 Of course you need to know how many bytes a record requires. The sizeof oper- sizeof ator can be used to determine the number of bytes needed for an object of a class or struct. Actually sizeof can be applied to any type object or value. It returns the size of its argument in bytes. The operator .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.