C++ File Management: Introduction & Examples - PDF

Summary

This document provides an introduction to C++ file management, covering concepts like file I/O operations, text and binary files and stream input and output. It demonstrates how to create, open, write, read, and close files in C++.

Full Transcript

CHAPTER FOUR File and file management Outline File and file management Streams and Files(Streams, Files, The standard streams) C++ File I/O Classes and Functions Text and Binary Files Text File processing Opening and Closing a file Obta...

CHAPTER FOUR File and file management Outline File and file management Streams and Files(Streams, Files, The standard streams) C++ File I/O Classes and Functions Text and Binary Files Text File processing Opening and Closing a file Obtaining the Current File Position I/O Status Buffers and Synchronization 12/27/2024 2 5.1. File and file management-Introduction C++ provides some classes to perform output and input of characters to/from files The information / data stored under a specific name on a storage device. A storage unit on a computer that stores information, data, etc. (such as a document, an image, etc.). Folders contain files Files have names and are represented by various icons At the lowest level, a file in C++ is interpreted simply as a sequence of bytes. The records in a file can be arranged in the following three ways: Ascending/Descending order: The records in the file can be arranged according to ascending or descending order of a key field. Alphabetical order: If the key field is of alphabetic type then the records are arranged in alphabetical order. 12/27/2024 3 File and file management-Introduction… Chronological order: In this type of order, the records are stored in the order of their occurrence i.e. arranged according to dates or events. If the key-field is a date, i.e., date of birth, date of joining, etc. then this type of arrangement is used. In C++, a stream is a data flow from a source to a sink. The sources and sinks can be any of the input/output devices or files. For input and output, there are two different streams called input stream and output stream. Stream Description – cin standard input stream – cout standard output stream – cerr standard error stream The standard source and sink are keyboard and monitor screen respectively. 12/27/2024 4 File Management System File Management: The process and act of creating an organized structure in which you store information for easy retrieval. It is a type of software that manages data files in a computer system. Has limited capabilities and is designed to manage individual or group files, such as special office documents and records. The system may contain features like: Assigning queued document numbers for processing. Owner and process mapping to track various stages of processing. Report generation, Notes, Status. Create, modify, move, copy, delete and other file operations, Add or edit basic metadata. 12/27/2024 5 What is File Handling in C++? The basic entity that stores the user's relevant data is called a file. Files can have a lot of types that are depicted by their extensions. For example :.txt(text file),.cpp(c++ source file),.exe(executable file),.pdf(portable document file) and many more. File Handling stands for the manipulation of files storing relevant data using a programming language, which is C++ in our case. This enables us to store the data in permanent storage even after the program performs file handling for the same ends of its execution. C++ offers the library fstream for file handling. Here we will discuss the classes through which we can perform I/O operations on files. 12/27/2024 6 File and file management-Introduction… For taking input from the keyboard and printing something on the console, you might have been using the cin (character input stream) and cout (character output stream) of the istream and ostream classes. File streams are similar to them. Only the console here is replaced by a file. Operations in File Handling: – Creating a file: open() – Reading data: read() – Writing new data: write() – Closing a file: close() 12/27/2024 7 General File I/O Steps 1. Include the header file fstream in the program. 2. Declare file stream variables. 3. Associate the file stream variables with the input/output sources. 4. Open the file 5. Use the file stream variables with >>, >variable". 12/27/2024 34 5.8. Buffers and Synchronization When we operate with file streams, these are associated to an internal buffer object of type streambuf. This buffer object may represent a memory block that acts as an intermediary between the stream and the physical file. For example, with an ofstream, each time the member function put (which writes a single character) is called, the character may be inserted in this intermediate buffer instead of being written directly to the physical file with which the stream is associated. When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream). This process is called synchronization and takes place under any of the following circumstances: 12/27/2024 35 When the file is closed: before closing a file, all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium. When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized. Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl. Explicitly, with member function sync(): Calling the stream's member function sync() causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0. 12/27/2024 36 37

Use Quizgecko on...
Browser
Browser