File reading and writing method, device, equipment and medium
Patent Information
- Application Number
- CN202410471323.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-16
- Publication Date
- 2026-09-08
- Estimated Expiration
- 2044-04-16
AI Technical Summary
[0003]现有技术中,完成文件读写操作的过程中,代码重用性差,应用繁琐,无法支持线程安全访问,即现有技术中读写操作为非原子操作,所以需要线程锁来实现安全访问等
[0038] The beneficial effects of this application are as follows: First, this application allocates a first memory block corresponding to the file information and stores the virtual file information structure generated by the fopen function in the first memory block; second, it allocates a second memory block corresponding to the file read/write pointer and stores the file structure generated by the fopen function in the second memory block; third, it allocates a third memory block in the file structure for storing data, and initializes the file structure and the virtual file information structure to obtain the target file; if a write operation is performed on the target file, the write start pointer is used, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file; if a read operation is performed on the target file, the read start pointer is used, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file. Therefore, before reading and writing the file in this application, a virtual file information structure is generated, and the first memory block, the second memory block, and the third memory block are allocated respectively. This modular encapsulation makes the code clearer and easier to maintain, thus making the software easy to understand and allowing for unlimited reuse of the code, making the application more convenient. Furthermore, when implementing read and write operations, calling the fwrite function to perform a WriteOffset operation on the first target data and calling the fread function to perform a ReadOffset operation on the second target data are both atomic operations. Therefore, there is no need for the system to disable interrupts or use thread locks, which can support thread-safe access.
Smart Images

Figure CN118260241B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to methods, apparatus, devices and media for reading and writing files. Background Technology
[0002] In embedded software development, objects are often virtualized as files, and read and write operations are performed on these files for data communication between two software modules, such as communication between interrupt functions and the main function, or between threads. These objects can be FIFO (First-In-First-Out Queue) queues, serial ports, etc. Designing a simple and reusable data structure can greatly improve software development efficiency and stability. A compact design can save CPU (Central Processing Unit) memory (Random-Access Memory, or RAM) space.
[0003] In existing technologies, the process of completing file read and write operations suffers from poor code reusability, cumbersome application, and inability to support thread-safe access. That is, in existing technologies, read and write operations are non-atomic operations, so thread locks are needed to achieve safe access.
[0004] In summary, how to implement file read and write operations that support thread-safe access and are convenient for applications is a problem that needs to be solved in this field. Summary of the Invention
[0005] In view of this, the purpose of this invention is to provide a file reading and writing method, apparatus, device, and medium that enables file reading and writing operations that support thread-safe access and are convenient to use. The specific solution is as follows:
[0006] Firstly, this application discloses a file reading and writing method, including:
[0007] Allocate a first memory block corresponding to the file information, and store the virtual file information structure generated by the fopen function in the first memory block;
[0008] Allocate a second memory block corresponding to the file read / write pointer, and store the file structure generated by the fopen function in the second memory block;
[0009] A third memory block for storing data is allocated in the file structure, and the file structure and the virtual file information structure are initialized to obtain the target file;
[0010] If a write operation is performed on the target file, the write start pointer is used as the basis, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file;
[0011] If a read operation is performed on the target file, the read start pointer is used as the basis, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file.
[0012] Optionally, the step of allocating a third memory block in the file structure for storing data includes:
[0013] Receive the user-defined memory size and, based on the defined memory size, request a third memory block in the file structure to store data.
[0014] Optionally, initializing the file structure and the virtual file information structure to obtain the target file includes:
[0015] The read index, write index, and virtual file information structure in the file structure are initialized, and a file operation handle is generated to obtain the target file, so that file read and write operations can be performed on the target file based on the file operation handle; wherein, the file operation handle points to the initialized virtual file information structure.
[0016] Optionally, the step of performing a WriteOffset operation on the first target data based on the write start pointer and calling the fwrite function includes:
[0017] Determine the current writable data length of the target file, and determine whether the current writable data length is greater than the data length of the first target data;
[0018] If so, then based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the first target data.
[0019] Optionally, the step of performing a WriteOffset operation on the first target data based on the write start pointer and calling the fwrite function includes:
[0020] Determine the dlen and dbuf of the first target data; wherein, dlen is used to characterize the data length of the first target data, and dbuf is the data content of the first target data;
[0021] Write the dlen to the target file, and determine the write start pointer of the dlen of the first target data;
[0022] Based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the dbuf.
[0023] Optionally, the step of reading the start pointer and calling the fread function to perform a ReadOffset operation on the second target data includes:
[0024] The read length of the custom second target data is stored in the rlen variable, and the position of the rlen variable in the target file is determined as the read start pointer;
[0025] Based on the read start pointer and the rlen variable, the fread function is called to perform a ReadOffset operation on the second target data.
[0026] Optionally, the file read / write method further includes:
[0027] If a file close command is received, the fclose function is called to release the first memory block, the second memory block, and the third memory block.
[0028] Secondly, this application discloses a file reading and writing device, comprising:
[0029] The first application module is used to apply for a first memory block corresponding to the file information, and to store the virtual file information structure generated by the fopen function in the first memory block;
[0030] The second application module is used to apply for a second memory block corresponding to the file read / write pointer, and to store the file structure generated by the fopen function in the second memory block;
[0031] The third application module is used to apply for a third memory block for storing data in the file structure, and to initialize the file structure and the virtual file information structure to obtain the target file;
[0032] The file write operation module is used to write the first target data to the target file by calling the fwrite function based on the write start pointer if a write operation is to be performed on the target file.
[0033] The file read operation module is used to read the second target data from the target file by calling the fread function based on the read start pointer if a read operation is to be performed on the target file.
[0034] Thirdly, this application discloses an electronic device, including:
[0035] Memory, used to store computer programs;
[0036] A processor is used to execute the computer program to implement the steps of the aforementioned disclosed file read / write method.
[0037] Fourthly, this application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the steps of the aforementioned disclosed file reading and writing method.
[0038] The beneficial effects of this application are as follows: First, this application allocates a first memory block corresponding to the file information and stores the virtual file information structure generated by the fopen function in the first memory block; second, it allocates a second memory block corresponding to the file read / write pointer and stores the file structure generated by the fopen function in the second memory block; third, it allocates a third memory block in the file structure for storing data, and initializes the file structure and the virtual file information structure to obtain the target file; if a write operation is performed on the target file, the write start pointer is used, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file; if a read operation is performed on the target file, the read start pointer is used, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file. Therefore, before reading and writing the file in this application, a virtual file information structure is generated, and the first memory block, the second memory block, and the third memory block are allocated respectively. This modular encapsulation makes the code clearer and easier to maintain, thus making the software easy to understand and allowing for unlimited reuse of the code, making the application more convenient. Furthermore, when implementing read and write operations, calling the fwrite function to perform a WriteOffset operation on the first target data and calling the fread function to perform a ReadOffset operation on the second target data are both atomic operations. Therefore, there is no need for the system to disable interrupts or use thread locks, which can support thread-safe access. Attached Figure Description
[0039] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0040] Figure 1This is a flowchart of a document reading and writing method disclosed in this application;
[0041] Figure 2 This is a specific file queue structure diagram disclosed in this application;
[0042] Figure 3 This is a schematic diagram illustrating a specific target file generation method disclosed in this application;
[0043] Figure 4 This is a specific writing operation diagram disclosed in this application;
[0044] Figure 5 This is a schematic diagram of a specific read operation disclosed in this application;
[0045] Figure 6 This is a schematic diagram of the structure of a document reading and writing device disclosed in this application;
[0046] Figure 7 This is a structural diagram of an electronic device disclosed in this application. Detailed Implementation
[0047] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.
[0048] In embedded software development, objects are often virtualized as files, and read and write operations are performed on these files for data communication between two software modules, such as communication between interrupt functions and the main function, or between threads. These objects can be, for example, FIFO Queues or serial ports. Designing a simple and reusable data structure can greatly improve software development efficiency and stability, and a compact design can save CPU memory space.
[0049] In existing technologies, the process of completing file read and write operations suffers from poor code reusability, cumbersome application, and inability to support thread-safe access. That is, in existing technologies, read and write operations are non-atomic operations, so thread locks are needed to achieve safe access.
[0050] Therefore, this application provides a file read / write scheme that enables file read / write operations that support thread-safe access and are convenient for applications.
[0051] See Figure 1 As shown in the figure, this application discloses a file reading and writing method, including:
[0052] Step S11: Allocate a first memory block corresponding to the file information, and store the virtual file information structure generated by the fopen function in the first memory block.
[0053] The first memory block is dynamically allocated to store the virtual file information structure (FileInfoSt) generated by the fopen function. The fopen function will return a handle fhandle to the caller, which points to the first memory block. The fclose function will release the first memory block later.
[0054] It is understood that in this embodiment, the object is virtualized as a file; the object can be a serial port or a queue, for example. Figure 2 The diagram shows a specific file queue structure. Taking the object as a first-in-first-out queue as an example, the fopen function has two parameters, type and size. If the object is a first-in-first-out queue, then the parameter type is FIFO Queue, and the parameter size is the size of the queue data buffer space.
[0055] Step S12: Allocate a second memory block corresponding to the file read / write pointer, and store the file structure generated by the fopen function in the second memory block.
[0056] A second memory block is dynamically allocated to store the file structure generated by the fopen function. It can be understood that if the object is a first-in-first-out queue, then the queue structure is stored in the second memory block. This structure is a member of FileInfoSt and manages the read and write operations of the file structure.
[0057] Step S13: Allocate a third memory block for storing data in the file structure, and initialize the file structure and the virtual file information structure to obtain the target file.
[0058] In this embodiment, requesting a third memory block for storing data in the file structure includes: receiving a user-defined memory size, and requesting a third memory block for storing data in the file structure based on the user-defined memory size. It is understood that the size of the third memory block can be customized by the user according to specific circumstances. It should be noted that the user can also customize the sizes of the first and second memory blocks according to specific application requirements.
[0059] In this embodiment, initializing the file structure and the virtual file information structure to obtain the target file includes: initializing the read index and write index in the file structure and the virtual file information structure, and generating a file operation handle to obtain the target file, so as to perform file read and write operations on the target file based on the file operation handle; wherein, the file operation handle points to the initialized virtual file information structure. For example Figure 3 The diagram illustrates a specific target file generation process, taking a first-in-first-out queue as an example. A third memory block (of size `size`) is dynamically allocated and pointed to the `Queue.buf` member `Queue.buf = buf`. This buffer is used to store read and write data, and its size is assigned, such as `Queue.size = size`. Other members of the `Queue` structure (i.e., the queue structure) are initialized, specifically the read and write indices. Then, the virtual file information structure `FileInfoSt` is initialized, and a pointer `fhandle` pointing to the initialized virtual file information structure is returned, serving as the file operation handle. This ensures that subsequent read and write operations are completed within the initialized virtual file information structure pointed to by the file operation handle.
[0060] Step S14: If a write operation is to be performed on the target file, the write start pointer is used as the basis, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file.
[0061] In this embodiment, the step of calling the fwrite function to perform a WriteOffset operation on the first target data based on the write start pointer includes: determining the current writable data length of the target file, and determining whether the current writable data length is greater than the data length of the first target data; if so, then calling the fwrite function to perform a WriteOffset operation on the first target data based on the write start pointer.
[0062] For example Figure 4 The diagram illustrates a specific write operation. It calculates the currently writable data length (unused) of the target file, taking a FIFO queue as an example. The remaining buffer (buf) length of the FIFO queue is calculated using the following formula:
[0063] unused=queue->size-(queue->write-queue->read);
[0064] Here, queue is a member of FileInfoSt called Queue. The function is expanded to use pointer operations on its member variables. queue->write and queue->read represent the write pointer and read pointer, respectively. It checks whether the current writable data length (unused) is greater than the data length of the first target data. If it is not greater, the write operation cannot be performed and the thread exits. If it is greater, it calls the fwrite function to perform a WriteOffset operation on the first target data based on the write start pointer.
[0065] In this embodiment, the step of calling the `fwrite` function to perform a `WriteOffset` operation on the first target data based on the write start pointer includes: determining the `dlen` and `dbuf` of the first target data; wherein, `dlen` is used to represent the data length of the first target data, and `dbuf` is the data content of the first target data; writing `dlen` to the target file, and determining the write position of `dlen` of the first target data as the write start pointer; and performing a `WriteOffset` operation on `dbuf` based on the write start pointer and calling the `fwrite` function. It should be noted that the complete first target data is `dlen` (4 bytes) + `dbuf` (dlen bytes), where `dlen` represents the data length of the first target data (4 bytes), and `dbuf` is the data content of the first target data. When writing the first target data to the target file, both `dlen` and `dbuf` are written to the target file. Therefore, only when `unused` is greater than `dbuf` + 4 bytes can subsequent write operations be performed.
[0066] First, `dlen` is written to the target file. Specifically, the function `queue_write` writes the length `dlen` as data into the `buf` pointer managed by the Queue, with a length of 4 bytes. Second, the write position of the first target data `dlen` is determined as the write start pointer. Based on the write start pointer, the function `fwrite` is called to perform a `WriteOffset` operation on `dbuf`. This means the function `queue_write` writes the data pointed to by the data pointer `dbuf` into the `buf` pointer managed by the Queue, with a length of `dlen`. The function `queue_write` performs a write data copy function managed by the Queue. `WriteOffset` is a modulo operation, which will not go out of bounds. That is, it will not exceed the memory range pointed to by `queue->buf`. The function `queue_write` performs modulo operations within the `Queue.size` range, thus avoiding pointer out-of-bounds issues. Figure 4As shown, the queue_write function operates on only one counter, that is, the queue_write function only operates on the write counter; it can be safely accessed in the system software module, and it is an atomic operation, so there is no need for the system to disable interrupts or use thread locks.
[0067] Step S15: If a read operation is to be performed on the target file, the fread function is called based on the read start pointer to perform a ReadOffset operation on the second target data in order to read the second target data from the target file.
[0068] In this embodiment, the step of performing a ReadOffset operation on the second target data based on the read start pointer and calling the fread function includes: storing the custom read length of the second target data in the rlen variable, and determining the position of the rlen variable in the target file as the read start pointer; and performing a ReadOffset operation on the second target data based on the read start pointer and the rlen variable and calling the fread function.
[0069] like Figure 5 The diagram illustrates a specific read operation, using a first-in-first-out queue as an example. The formula for calculating the length of unread data (used) is as follows:
[0070] used=queue->write-queue->read;
[0071] If the length of the unread data, `used`, is equal to 0, it means that no reading operation is needed at this time, and the process exits and returns with a length of 0. If the length of the unread data, `used`, is not equal to 0, then a reading operation is needed. This involves using the read start pointer and calling the `fread` function to perform a `ReadOffset` operation on the second target data to read the second target data from the target file. The specific process is as follows:
[0072] 1) Store the length of the custom second target data to be read in the rlen variable. That is, the length of the data read by calling the queue_read function is stored in the variable rlen, which is the length of the data read in this time.
[0073] 2) Determine the position of the rlen variable in the target file as the read start pointer. Based on the read start pointer and the rlen variable, call the fread function to perform a ReadOffset operation on the second target data, that is, call the queue_read function to read a frame of data with a length of rlen and return the length rlen.
[0074] It's understandable that the function `queue_read` performs data copying and reading within the `Queue` class, and `ReadOffset` performs a modulo operation, ensuring it won't exceed memory boundaries. In other words, it won't exceed the memory range pointed to by `queue->buf`. Figure 5 As shown, the function queue_read performs modulo operations within the Queue.size range, which will not cause pointer out-of-bounds issues. The queue_read function only operates on the read counter. It can be seen that queue_write only operates on the write counter, and queue_read only operates on the read counter. They can be safely accessed in two different software modules that are preempted by the system. They are all atomic operations and do not require the system to disable interrupts or use thread locks.
[0075] In this embodiment, the method further includes: if a file close command is received, calling the fclose function to release the first memory block, the second memory block, and the third memory block. When no read / write operations are required, upon receiving a file close command, the file close operation corresponding to the fclose function is completed in the FileInfoSt pointed to by fhandle, and the dynamic memory allocated by the fopen function is released in reverse order.
[0076] The beneficial effects of this application are as follows: First, this application allocates a first memory block corresponding to the file information and stores the virtual file information structure generated by the fopen function in the first memory block; second, it allocates a second memory block corresponding to the file read / write pointer and stores the file structure generated by the fopen function in the second memory block; third, it allocates a third memory block in the file structure for storing data, and initializes the file structure and the virtual file information structure to obtain the target file; if a write operation is performed on the target file, the write start pointer is used, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file; if a read operation is performed on the target file, the read start pointer is used, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file. Therefore, before reading and writing the file in this application, a virtual file information structure is generated, and the first memory block, the second memory block, and the third memory block are allocated respectively. This modular encapsulation makes the code clearer and easier to maintain, thus making the software easy to understand and allowing for unlimited reuse of the code, making the application more convenient. Furthermore, when implementing read and write operations, calling the fwrite function to perform a WriteOffset operation on the first target data and calling the fread function to perform a ReadOffset operation on the second target data are both atomic operations. Therefore, there is no need for the system to disable interrupts or use thread locks, which can support thread-safe access.
[0077] See Figure 6 As shown in the figure, this application discloses a file reading and writing device, including:
[0078] The first application module 11 is used to apply for a first memory block corresponding to the file information, and to store the virtual file information structure generated by the fopen function in the first memory block;
[0079] The second application module 12 is used to apply for a second memory block corresponding to the file read / write pointer, and to store the file structure generated by the fopen function in the second memory block;
[0080] The third application module 13 is used to apply for a third memory block for storing data in the file structure, and to initialize the file structure and the virtual file information structure to obtain the target file;
[0081] The file write operation module 14 is used to, if a write operation is to be performed on the target file, call the fwrite function based on the write start pointer to perform a WriteOffset operation on the first target data, so as to write the first target data into the target file;
[0082] The file read operation module 15 is used to read the second target data from the target file by calling the fread function based on the read start pointer if a read operation is to be performed on the target file.
[0083] The beneficial effects of this application are as follows: First, this application allocates a first memory block corresponding to the file information and stores the virtual file information structure generated by the fopen function in the first memory block; second, it allocates a second memory block corresponding to the file read / write pointer and stores the file structure generated by the fopen function in the second memory block; third, it allocates a third memory block in the file structure for storing data, and initializes the file structure and the virtual file information structure to obtain the target file; if a write operation is performed on the target file, the write start pointer is used, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file; if a read operation is performed on the target file, the read start pointer is used, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file. Therefore, before reading and writing the file in this application, a virtual file information structure is generated, and the first memory block, the second memory block, and the third memory block are allocated respectively. This modular encapsulation makes the code clearer and easier to maintain, thus making the software easy to understand and allowing for unlimited reuse of the code, making the application more convenient. Furthermore, when implementing read and write operations, calling the fwrite function to perform a WriteOffset operation on the first target data and calling the fread function to perform a ReadOffset operation on the second target data are both atomic operations. Therefore, there is no need for the system to disable interrupts or use thread locks, which can support thread-safe access.
[0084] Furthermore, embodiments of this application also provide an electronic device. Figure 7 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content of the diagram should not be construed as limiting the scope of this application.
[0085] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Specifically, it may include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the following steps:
[0086] Allocate a first memory block corresponding to the file information, and store the virtual file information structure generated by the fopen function in the first memory block;
[0087] Allocate a second memory block corresponding to the file read / write pointer, and store the file structure generated by the fopen function in the second memory block;
[0088] A third memory block for storing data is allocated in the file structure, and the file structure and the virtual file information structure are initialized to obtain the target file;
[0089] If a write operation is performed on the target file, the write start pointer is used as the basis, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file;
[0090] If a read operation is performed on the target file, the read start pointer is used as the basis, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file.
[0091] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:
[0092] Receive the user-defined memory size and, based on the defined memory size, request a third memory block in the file structure to store data.
[0093] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:
[0094] The read index, write index, and virtual file information structure in the file structure are initialized, and a file operation handle is generated to obtain the target file, so that file read and write operations can be performed on the target file based on the file operation handle; wherein, the file operation handle points to the initialized virtual file information structure.
[0095] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:
[0096] Determine the current writable data length of the target file, and determine whether the current writable data length is greater than the data length of the first target data;
[0097] If so, then based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the first target data.
[0098] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:
[0099] Determine the dlen and dbuf of the first target data; wherein, dlen is used to characterize the data length of the first target data, and dbuf is the data content of the first target data;
[0100] Write the dlen to the target file, and determine the write start pointer of the dlen of the first target data;
[0101] Based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the dbuf.
[0102] In some specific embodiments, the processor executes a computer program stored in the memory, specifically implementing the following steps:
[0103] The read length of the custom second target data is stored in the rlen variable, and the position of the rlen variable in the target file is determined as the read start pointer;
[0104] Based on the read start pointer and the rlen variable, the fread function is called to perform a ReadOffset operation on the second target data.
[0105] In some specific embodiments, the processor, by executing a computer program stored in the memory, may further include the following steps:
[0106] If a file close command is received, the fclose function is called to release the first memory block, the second memory block, and the third memory block.
[0107] In this embodiment, the power supply 23 is used to provide operating voltage for various hardware devices on the electronic device; the communication interface 24 can create a data transmission channel between the electronic device and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of this application, and is not specifically limited here; the input / output interface 25 is used to acquire external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs, and is not specifically limited here.
[0108] The processor 21 may include one or more processing cores, such as a quad-core processor or an octa-core processor. The processor 21 may be implemented using at least one hardware form selected from DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), and PLA (Programmable Logic Array). The processor 21 may also include a main processor and a coprocessor. The main processor, also known as a CPU (Central Processing Unit), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 21 may integrate a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, the processor 21 may also include an AI (Artificial Intelligence) processor, which is used to handle computational operations related to machine learning.
[0109] In addition, the memory 22, as a carrier for resource storage, can be a read-only memory, random access memory, disk or optical disk, etc. The resources stored on it include operating system 221, computer program 222 and data 223, etc., and the storage method can be temporary storage or permanent storage.
[0110] The operating system 221 manages and controls the various hardware devices and computer programs 222 on the electronic device to enable the processor 21 to perform calculations and processing on the massive amounts of data 223 in the memory 22. The operating system can be Windows, Unix, Linux, etc. The computer program 222, in addition to including computer programs capable of performing the file reading and writing methods disclosed in any of the foregoing embodiments, may further include computer programs capable of performing other specific tasks. The data 223 may include data received by the electronic device from external devices, as well as data collected by its own input / output interface 25.
[0111] Furthermore, this application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, it implements the aforementioned file read / write method. Specific steps of this method can be found in the corresponding content disclosed in the foregoing embodiments, and will not be repeated here.
[0112] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section.
[0113] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application. The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly in hardware, software modules executed by a processor, or a combination of both. The software module may be located in random access memory (RAM), memory, read-only memory (ROM), electrically programmable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), registers, hard disks, removable disks, CD-ROMs (CoS24P000617act Disc Read-Only Memory), or any other form of storage medium known in the art.
[0114] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0115] The foregoing has provided a detailed description of a file reading and writing method, apparatus, device, and medium provided by the present invention. Specific examples have been used to illustrate the principles and implementation methods of the present invention. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of the present invention. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of the present invention. Therefore, the content of this specification should not be construed as a limitation of the present invention.
Claims
1. A file reading and writing method, characterized in that, include: Allocate a first memory block corresponding to the file information, and store the virtual file information structure generated by the fopen function in the first memory block; Allocate a second memory block corresponding to the file read / write pointer, and store the file structure generated by the fopen function in the second memory block; A third memory block for storing data is allocated in the file structure, and the file structure and the virtual file information structure are initialized to obtain the target file; If a write operation is performed on the target file, the write start pointer is used as the basis, and the fwrite function is called to perform a WriteOffset operation on the first target data to write the first target data into the target file; If a read operation is performed on the target file, the read start pointer is used as the basis, and the fread function is called to perform a ReadOffset operation on the second target data to read the second target data from the target file.
2. The file reading and writing method according to claim 1, characterized in that, The step of requesting a third memory block in the file structure for storing data includes: Receive the user-defined memory size and, based on the defined memory size, request a third memory block in the file structure to store data.
3. The file reading and writing method according to claim 1, characterized in that, The initialization of the file structure and the virtual file information structure to obtain the target file includes: The read index, write index, and virtual file information structure in the file structure are initialized, and a file operation handle is generated to obtain the target file, so that file read and write operations can be performed on the target file based on the file operation handle; wherein, the file operation handle points to the initialized virtual file information structure.
4. The file reading and writing method according to claim 1, characterized in that, The step of performing a WriteOffset operation on the first target data based on the write start pointer and calling the fwrite function includes: Determine the current writable data length of the target file, and determine whether the current writable data length is greater than the data length of the first target data; If so, then based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the first target data.
5. The file reading and writing method according to claim 4, characterized in that, The step of performing a WriteOffset operation on the first target data based on the write start pointer and calling the fwrite function includes: Determine the dlen and dbuf of the first target data; wherein, dlen is used to characterize the data length of the first target data, and dbuf is the data content of the first target data; Write the dlen to the target file, and determine the write start pointer of the dlen of the first target data; Based on the write start pointer, the fwrite function is called to perform a WriteOffset operation on the dbuf.
6. The file reading and writing method according to claim 1, characterized in that, The step of reading the starting pointer and calling the fread function to perform a ReadOffset operation on the second target data includes: The read length of the custom second target data is stored in the rlen variable, and the position of the rlen variable in the target file is determined as the read start pointer; Based on the read start pointer and the rlen variable, the fread function is called to perform a ReadOffset operation on the second target data.
7. The file reading and writing method according to any one of claims 1 to 6, characterized in that, Also includes: If a file close command is received, the fclose function is called to release the first memory block, the second memory block, and the third memory block.
8. A file reading and writing device, characterized in that, include: The first application module is used to apply for a first memory block corresponding to the file information, and to store the virtual file information structure generated by the fopen function in the first memory block; The second application module is used to apply for a second memory block corresponding to the file read / write pointer, and to store the file structure generated by the fopen function in the second memory block; The third application module is used to apply for a third memory block for storing data in the file structure, and to initialize the file structure and the virtual file information structure to obtain the target file; The file write operation module is used to write the first target data to the target file by calling the fwrite function based on the write start pointer if a write operation is to be performed on the target file. The file read operation module is used to read the second target data from the target file by calling the fread function based on the read start pointer if a read operation is to be performed on the target file.
9. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for executing the computer program to implement the steps of the file read / write method as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, Used to store computer programs; wherein, when the computer programs are executed by a processor, they implement the steps of the file read / write method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
STDF file read-write method, medium, equipment and device
CN113312322A
File read-write method and device, equipment, storage medium and file access controller
CN117827116A