生活随笔
收集整理的這篇文章主要介紹了
C语言fscanf函数了解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
fscanf函數(shù)從一個(gè)流中執(zhí)行格式化輸入,fscanf遇到空格和換行時(shí)結(jié)束,注意空格時(shí)也結(jié)束。這與fgets有區(qū)別,fgets遇到空格不結(jié)束。
原型:int fscanf(FILE *stream, char *format,[argument...]);
返回值:返回實(shí)際被轉(zhuǎn)換并賦值的輸入項(xiàng)的數(shù)目。
%d:讀入一個(gè)十進(jìn)制整數(shù)。
%i :讀入十進(jìn)制,八進(jìn)制,十六進(jìn)制整數(shù),與%d類(lèi)似,但是在編譯時(shí)通過(guò)數(shù)據(jù)前置來(lái)區(qū)分進(jìn)制,如加入“0x”則是十六進(jìn)制,加入“0”則為八進(jìn)制。例如串“031”使用%d時(shí)會(huì)被算作31,但是使用%i時(shí)會(huì)算作25。
scanf(...)函數(shù)與fscanf(stdin,...)相同。
sscanf(s,...)函數(shù)與scanf(...)等價(jià),所不同的是,前者的輸入字符來(lái)源于字符串s.
------------------------------------------
下面是百科中的兩個(gè)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??
總結(jié)
以上是生活随笔為你收集整理的C语言fscanf函数了解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。