In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the processing method of random access files in C++". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the processing method of random access files in C++".
The basic file operations are
◆ fopen-- opens the file, specifying how the file is opened (read / write) and type (binary / text)
◆ fclose-- closes an open file
◆ fread-- reads files
◆ fwrite-- writes files
◆ fseek/fsetpos-- transfers the file indicator to a place in the file
◆ ftell/fgetpos-- can tell you where the file indicator is.
There are two basic types of files: text and binary. Of the two, binary types are usually easier to solve. Since it is not common to deal with random access in text, we will focus on binary file processing in this article. The first four of the operations listed above can be used for text files and random access files. The last two items are for random access only.
Random access means that we can switch between any part of a file, and we can read and write data from it without having to read through the entire file.
Binary file
A binary is a file of any length that holds byte values ranging from 0 to 0xff (0 to 255). Unlike these bytes, which have no meaning in binary files, in text files, a value of 13 means carriage return, 10 means line wrapping, and 26 means the end of the file, and the software that reads the text file should be able to solve these problems.
In today's terminology, we refer to binaries as character streams containing bytes, and most languages tend to understand them as character streams rather than files. The important part is the data flow itself, not its source. In C, you can consider data in terms of files or data streams. Or, you can think of it as an array of leaders. With random access, you can read and write any part of the array.
Example 1:
/ / ex1.c: Defines the entry point for the console application.
/ /
# include
< stdio.h># include
< string.h># include
< windows.h>Int FileSuccess (FILE * handle,const char * reason, const char * path) {
OutputDebugString (reason)
OutputDebugString (path)
OutputDebugString ("Result:")
If (handle==0)
{
OutputDebugString ("Failed")
Return 0
}
Else
{
OutputDebugString ("Suceeded")
Return 1
}
}
Int main (int argc, char * argv [])
{
Const char * filename= "test.txt"
Const char * mytext= "Once upon a time there were three bears."
Int byteswritten=0
FILE * ft= fopen (filename, "wb")
If (FileSuccess (ft, "Opening File:", filename)) {
Fwrite (mytext,sizeof (char), strlen (mytext), ft)
Fclose (ft)
}
Printf ("len of mytext =% I", strlen (mytext))
Return 0
}
This code shows a simple opening of the binary file to be written to which the text character (char*) is written. Usually you use text files, but the author wants to prove that you can write text to binary files.
/ / ex1.c
The role of example 1
This example opens a binary file to be written. The FILE* variable is returned from the fopen () call. If this operation fails, it returns 0.
The Fopen () command attempts to open the specified file, which in this case is test.txt located in the same folder. Remember, if the file contains a path, then all backspaces must overlap. "c:\ folder\ test.txt" is wrong, you must use "c:\\ folder\\ test.txt".
Since the file style is wb, we are about to write to the binary file. If the file does not exist, the system will create a file, if it exists, the contents will be deleted. If the call to fopen fails, either because the file is opened, or its name includes invalid characters, or an invalid path, then fopen returns a value of 0.
Although you can just check if ft is 0 (a value of 0 is successful), the author added a FileSuccess () function to ensure this operation. In the window, it shows whether the call was successful and the file name. If you fail, you may need to fix it. Note that there is generally not much output text available to the system debugger in Windows
The fwrite (mytext,sizeof (char), strlen (mytext), ft); fwrite () call outputs the specified text. Second, the three parameters are the size of the character and the length of the string. Both of them are defined by size_t. Note that with the binaries, even if you are writing (char*) a string to the file, it does not have any additional newline characters. If you want these characters, you must explicitly include them in the string.
Read and write files
When you open a file, you must specify how to open it. This means that if you plan to attach something to a file, do you want to create a new file and overwrite it? Is it a text file or a binary file? Do you want to read the file or write the file? This is done by using one or more file pattern classifiers, which are separate letters "r", "b", "w", "a" and +. "r" means to open a file for reading. The operation will fail if the file does not exist or cannot be found. "w" means to open a file to be written or an empty file. If the file exists, the contents of the file will be corrupted. "a" means to open the file and prepare to write from the end of the file without removing the EOF tag before writing new data; if the file does not exist, a file will be created first. Adding + to the file model creates the following three new models:
"r +" opens a file waiting to be read or written. "w +" opens a file as an empty file for reading or writing. If the file exists, the contents of the file are corrupted.
"a +" opens a file waiting to be read or added, which includes the removal of the EOF tag before the new data is written, and the saving of the EOF tag after the write is completed. If the file does not exist, the file is created first.
The following list shows the combination of text and binary files. Usually you can choose to read or write from a text file, but do not use both.
In the case of binaries, you can choose to read and write to the same file. The list tells us what we can do with the word code.
Mode Type of file Read Write Create Truncate r text Read
In my experience, unless you have just finished creating or reading a file, you can only get away with it by using "wreckb".
There are other situations that allow other letters to exist. For example, Microsoft runs "t" for text mode, "c" for approval, "n" for non-approval, "S" for sequential access optimization buffer, "R" for random access, "T" for temporary and "D" for deletion / temporary storage.
The main reason for using binaries is flexibility; you can read or write to any part of the file. Text files only allow you to read or write in order. Now, with the popularity of SQLite or MySQL databases, the need for random access in binary files has been reduced. In a sense, random access file records are a little old-fashioned, but still useful.
The author used a variety of data processing schemes based on random access files before the popularity of databases. For example, in small files, the author uses the index / data file mode. The mode includes two files. One is the data file, which holds records of different lengths. Another type of file is an index file, which has the same record as a data file. But in the index file, each record is of the same length and consists of two parts that fit the structure.
Struct {?? fpos_t pos;??int size;} indexrec
The type fpos_t is an execution defined and used by fsetpos () and fgetpos (). These are newer versions of fseek and ftell and are more helpful in bookmarking. If you are calculating the file and need to configure the file, then you should use fseek (), and ftell () can also give you the current location of the int.
In practice, fpos_t may just be an int, but you should use the fpos_t type. It saves a copy of the current file indicator. This is the property of the random access file, which indicates the location of the next read or write. It has a granularity of one, so you can put it anywhere in the file.
Thank you for your reading. The above is the content of "what is the processing method of random access files in C++". After the study of this article, I believe you have a deeper understanding of the problem of how to deal with random access files in C++, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.