我正在看着關於fsacnf的msdn解釋,並試圖更改代碼..這是一場災難,我不明白它是如何工作的。 如果例如我有一個文件x有這樣的信息:「string」 7 3.13'x' 當我寫入scanf(「%s」,& string_input),以便字符串正在保存,然後它轉到下一行? - >到7?我現在會寫: char: char test; fscanf(「%c」,&測試) - 它會跳轉到'x'或取7並將其變成ascii值?fscanf如何與不同的變量類型一起工作?
這裏是我試過的代碼,輸出:
#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);
}
}
這是輸出:
a-string -858553460 8.000000 4 f
無法爲什麼
感謝理解!
點擊下一步你的選擇答案。 – hmjd 2012-07-18 13:21:58