我正在查看有关 fsacnf 的 msdn 解释并尝试更改代码。这是一场灾难,我不明白它是如何工作的。例如,如果我有一个包含此信息的文件 x:“string”7 3.13 ' x' 当我写 scanf("%s",&string_input) 所以字符串被保存然后它转到下一行?-> 到 7?我现在要写: char test; fscanf("%c" , &test) -- 它会跳转到 'x' 还是取 7 并将其转换为它的 ascii 值?
这是我尝试过的代码和输出:
#include <stdio.h>
FILE *stream;
int main( void )
{
long l;
float fp,fp1;
char s[81];
char c,t;
stream = fopen( "fscanf.out", "w+" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fprintf( stream, "%s %d %c%f%ld%f%c", "a-string",48,'y', 5.15,
65000, 3.14159, 'x' );
// Security caution!
// Beware loading data from a file without confirming its size,
// as it may lead to a buffer overrun situation.
/* Set pointer to beginning of file: */
fseek( stream, 0L, SEEK_SET );
/* Read data back from file: */
fscanf( stream, "%s", s );
fscanf( stream, "%c", &t );
fscanf( stream, "%c", &c );
fscanf( stream, "%f", &fp );
fscanf( stream, "%f", &fp1 );
fscanf( stream, "%ld", &l );
printf( "%s\n", s );
printf("%c\n" , t);
printf( "%ld\n", l );
printf( "%f\n", fp );
printf( "%c\n", c );
printf("f\n",fp1);
getchar();
fclose( stream );
}
}
这是输出:
字符串 -858553460 8.000000 4 F
不明白为什么
谢谢!!