生活随笔
收集整理的這篇文章主要介紹了
C语言fscanf函数了解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
fscanf函數從一個流中執行格式化輸入,fscanf遇到空格和換行時結束,注意空格時也結束。這與fgets有區別,fgets遇到空格不結束。
原型:int fscanf(FILE *stream, char *format,[argument...]);
返回值:返回實際被轉換并賦值的輸入項的數目。
%d:讀入一個十進制整數。
%i :讀入十進制,八進制,十六進制整數,與%d類似,但是在編譯時通過數據前置來區分進制,如加入“0x”則是十六進制,加入“0”則為八進制。例如串“031”使用%d時會被算作31,但是使用%i時會算作25。
scanf(...)函數與fscanf(stdin,...)相同。
sscanf(s,...)函數與scanf(...)等價,所不同的是,前者的輸入字符來源于字符串s.
------------------------------------------
下面是百科中的兩個DEMO
------------------------------------------
[cpp]?view plaincopy
?? ? ?? ?? ?? ?? #include?<stdio.h>?? #include?<stdlib.h>?? #define?FIRST_DEMO?? ?? #ifdef?FIRST_DEMO?? int?main(void)?? {?? ????int?i;?? ????printf("Input?an?integer:");?? ?????? ????if?(fscanf(stdin,"%d",&i))?? ????{?? ????????printf("The?integer?read?was?:%d\n",i);?? ????}??? ????else?? ????{?? ????????fprintf(stderr,"Error?reading?an?integer?from?stdin.\n");?? ????????exit(1);?? ????}?? ????system("pause");?? ????return?0;?? }?? #elif?defined?SECOND_DEMO?? ????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');????? ?????????????? ????????????fseek(stream,0L,SEEK_SET);?? ?????????????? ????????????fscanf(stream,"%s",s);?? ????????????fscanf(stream,"%ld",&l);?? ????????????fscanf(stream,"%f",&fp);?? ????????????fscanf(stream,"%c",&c);?? ?????????????? ????????????printf("%s\n",s);?? ????????????printf("%ld\n",l);?? ????????????printf("%f\n",fp);?? ????????????printf("c=%c\n",c);?? ????????????fclose(stream);?? ????????}?? ????????system("pause");?? ????????return?0;?? ????}?? ?? #endif??
總結
以上是生活随笔為你收集整理的C语言fscanf函数了解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。