Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to the application function of how to operate C++ files

Shulou Source: shulou.com Published: 2022-06-02 22:38:20 09月10日 Update

Today, I will talk to you about the introduction of the application function on how to operate C++ files, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

C++ programming language is applied flexibly and can be regarded as an upgraded version of C language. Through the relevant methods about the operation of C++ files introduced in this article, we can have a preliminary grasp of the relevant application skills of this computer programming language and deepen our understanding of the language.

1. Function function in file operation of Clipper +

Used to read and write a data block.

two。 General invocation form

Fread (buffer,size,count,fp)

Fwrite (buffer,size,count,fp)

3. Description

(1) buffer: it is a pointer, and for fread, it is the address where the read data is stored. For fwrite, the address to which the data is to be output.

(2) size: the number of bytes to read and write

(3) count: how many size bytes of data items to read and write

(4) fp: file pointer.

Note: 1 after completing the write operation (fwrite ()), you must close the stream (fclose ())

2 after completing a C++ file operation (fread ()), if the stream (fclose ()) is not closed, the pointer (FILE * fp) automatically moves backward the length of the previous read and write. If the stream is not closed for the next read operation, the output continues with the last output.

3 fprintf (): input to the stream in format, the prototype is int fprintf (FILE * stream, const char * format [, argument,...]); its usage is the same as printf (), but not to the console, but to the stream. Note that the return value is the number of bytes written to the file for this operation. For example, int c = fprintf (fp, "% s% s% d% f", str1,str2, a, b); str1:10 bytes; str2: 10 bytes; aRO 2 bytes; BRV 8 bytes, c is 33, because a space is automatically added between different data when writing.

The file must be closed after use, otherwise the content will not be displayed correctly. Fwrite: read in two student information and save the file with fwrite

Fread: use fread to read student information from the file.

Fwrite.c # include # define SIZE 2 struct student_type {char name [10]; int num; int age; char addr [10];} stud [SIZE]; void save () {FILE * fp; int i; if ((fp=fopen ("stu_list", "wb")) = = NULL) {printf ("cant open the file"); exit (0);} for

Tags: File byte clocking data application content function pointer language output information address student article programming language programming different same two from Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker MariaDB MySQL Linux Apple