1
|
int
fscanf
(
FILE
*stream,
char
*format,[argument...]);
|
1
2
3
4
5
|
FILE
*fp;
char
a[10];
int
b;
double
c;
fscanf
(fp,
"%s%d%lf"
,a,&b,&c)
|
fscanf用法:fscanf(fp,"%d",&var)
fscanf_s用法:fscanf(fp,"%d",&var,sizeof(int))
区别:fscanf_s需要指定长度
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/* FSCANF.C: This program writes formatted data to a file. It then uses fscanf to read the various data back from the file.*/
#include
FILE
*stream;
int
main(
void
)
{
long
l;
float
fp;
char
s[81];
char
c;
stream =
fopen
(
"fscanf.out"
,
"w+"
);
if
( stream == NULL )
printf
(
"The file fscanf.out was not opened\n"
);
else
{
fprintf
( stream,
"%s %ld %f%c"
,
"a-string"
,
65000, 3.14159,
'x'
);
/* Set pointer to beginning of file: */
fseek
( stream, 0L, SEEK_SET );
/* Read data back from file: */
fscanf
( stream,
"%s"
, s );
fscanf
( stream,
"%ld"
, &l );
fscanf
( stream,
"%f"
, &fp );
fscanf
( stream,
"%c"
, &c );
/* Output data read: */
printf
(
"%s\n"
, s );
printf
(
"%ld\n"
, l );
printf
(
"%f\n"
, fp );
printf
(
"%c\n"
, c );
fclose
( stream );
}
}
|