| %d | 有正負號的十進位整數 | int * |
| %i | 有正負號的十進位整數 | int * |
| %u | 無正負號的十進位整數 | int * |
| %o | 無正負號的八進位整數 | int * |
| %x, %X | 無正負號的十六進位整數 | int * |
| %c | 字元 | char |
| %s | 字串 | char * |
| %f | 浮點數 | double |
| %p | 記憶體位址的編碼 | void * |
| %% | 百分比符號 | % |
fscanf() 可接受多個參數,至少要有第一個指向結構 FILE 的指標,以及第二個所要輸入的格式化字串,隨後接依格式化字串中轉換格式的數量相對應的參數,這是說,如果格式化字串中用了三個轉換格式,其後就需要另外三個對應的參數。
以下程式示範 fscanf() 的使用,假設 oldname.txt 中有 "She is 19 years old." 的文字
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fPtr;
char c1[20], c2[20], c4[20], c5[20];
int c3;
fPtr = fopen("oldname.txt", "r");
if (!fPtr) {
printf("檔案開啟失敗...\n");
exit(1);
}
fscanf(fPtr, "%s%s%d%s%s", c1, c2, &c3, c4, c5);
fclose(fPtr);
printf("%s %s %d %s %s\n", c1, c2, c3, c4, c5);
return 0;
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:cfscanf.c
功能:示範 stdio.h 中函數 fscanf() 的使用
作者:張凱慶
時間:西元2010年6月 */編譯後執行,結果如下

您可以繼續參考
輸出與輸入 stdio.h
- int getchar(void);
- char *gets(char *s);
- int putchar(int c);
- int puts(const char *s);
- int printf(const char *format, ...);
- int sprintf(char *s, const char *format, ...);
- int scanf(const char *format, ...);
- int sscanf(char *s, const char *format, ...);
- FILE *fopen(const char *filename, const char *mode);
- FILE *freopen(const char *filename, const char *mode, FILE *stream);
- int fflush(FILE *stream);
- int fclose(FILE *stream);
- int remove(const char *filename);
- int rename(const char *oldname, const char *newname);
- int fprintf(FILE *stream, const char *format, ...);
- int fscanf(FILE *stream, const char *format, ...);
- int fgetc(FILE *stream);
- char *fgets(char *s, int n, FILE *stream);
- int fputs(const char *s, FILE *stream);
- int fputs(const char *s, FILE *stream);
- size_t fread(void *ptr, size_t size, siz_t nobj, FILE *stream)
- size_t fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream)
- int fseek(FILE *stream, long offset, int origin);
- long ftell(FILE *stream);
- void rewind(FILE *stream);
沒有留言:
張貼留言