Function: Performs formatted input from a stream, fscanf encounters a space and ends a newline, and ends when noting a space.
Usage: int fscanf (FILE *stream, char *format,[argument ...]);
int fscanf (file pointer, format string, input list);
For example:
FILE*FP;
CHARA[10];
Intb
Doublec;
FSCANF (FP, "%s%d%lf", A,&b,&c)
Return value: integer, value equals [argument ...] Number of
The format is the equivalent of the formatting in the regular expression, that is, what format separates the information in the file. The light is not good to understand, to use an example to illustrate the specific how to use:
First I have a data. TXT file inside the data format is as follows:
2,50,41,w,20.585828
4,52,51,r,52.012547
.........................
Many similar records are separated by
.......................
The function I implement is to assign five fields of the data in the above file to the corresponding five variables and output the values of those variables. The implementation of the program is as follows: #include <stdio.h>
#include <stdlib.h>
Int main ()
{
int fd;
Long Dev;
long offset;
long length;
Char ch;
Double ts=0.000000;
if ((Fd=fopen ("/home/haixian/ceshi/data.txt", "R") <0)
{
printf ("Open the file is error!\n");
exit (0);
&NBSP;}
Lseek (fd,0,seek_set);
while (5==FSCANF (FD, "%ld,%ld,%ld,%c,%lf\n",&dev,&offset,& Length,&ch,&ts)
{Here is the second parameter specifies the format of the separator parameter, which is used here to separate. This makes it easy to get the values of each field of the record without having to write a function to parse it or anything.
printf ("%ld,%ld,%ld,%c,%lf\n", dev,offset,length,ch,ts);
}
Close (FD);
return 0;
}