Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An example Analysis of the functions related to the Operation of C language File

Shulou Source: shulou.com Published: 2022-06-01 21:40:54 09月14日 Update

This article mainly explains "C language file operation related function example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "c language file operation related function example analysis" it!

On the usage of fscanf function

_ Check_return_ _ CRT_INSECURE_DEPRECATE (fscanf_s) _ CRTIMP int _ _ cdecl fscanf (_ Inout_ FILE * _ File, _ In_z_ _ Scanf_format_string_ const char * _ Format,...); this is the function declaration, which takes three arguments, the first is a file pointer, and the second is a format such as "% s% d." In this way, the third is the address where the data read from the file is saved. Specific these can go to Baidu, and then I would like to talk about my understanding of this. While (! feof (fp)) {fscanf (fp, "s% d", myp.myname, & myp.socer);}

You wonder why you can read the formatting characters in the file. This is because the file pointer automatically moves down, and every time we read a line, the file pointer automatically reads down a line. Something similar is the read function. (specific technical terms may be incorrect, I hope you will point out) examples are attached below

# include#include#includestruct MyStruct {char myname [10]; int socer;}; int main () {MyStruct myp;FILE * fp = NULL;fp = fopen ("d:/data.txt", "a +"); while (! feof (fp)) {fscanf (fp, "% s% d\ n", myp.myname, & myp.socer); printf ("% s% d\ n", myp.myname, myp.socer);} printf ("% s% d\ n", myp.myname, myp.socer) Fclose (fp); system ("pause"); return 0;}

Careful students may find that the fscanf () used in my example is a little different from that written earlier.

Fscanf (fp, "% s% d", myp.myname, & myp.socer); fscanf (fp, "% s% d\ n", myp.myname, & myp.socer)

Both of them can work. Having said so much, I just want to express that the file pointer will automatically move down, which you may already know.

First of all, you have to create data.txt content in the d disk like this Zhang San 100 Li Si 99 Wang 2 60 to this, I believe you have a deeper understanding of the "c language file operation-related function analysis", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File function pointer language analysis content one line three examples format this is learning mobile practical deeper careful curious and then professional two. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Docker macOS Shulou Tech Info