fscanf()函数

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

fscanf

函数名: fscanf

功能: 从一个流中执行格式化输入

用法: int fscanf(FILE *stream, char *format,[argument...]);

int fscanf(文件指针,格式字符串,输入列表);

返回值:整型,数值等于[argument...]的个数

程序例:

#include

#include

int main(void)

{

int i;

printf("Input an integer: ");

/* read an integer from the

standard input stream */

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);

}

return 0;

}

返回EOF如果读取到文件结尾。

常用基本参数对照:

'%d' : Scan an integer as a signed decimal number.

'%i' : Scan an integer as a signed number. Similar to '%d', but interprets the number as hexadecimal when preceded by "0x" and octal when preceded by "0". For example, the string "031" would be read as 31 using '%d', and 25 using '%i'.

'%u' : Scan for decimal unsigned int, unsigned short, and unsigned char

'%f' : Scan a floating-point number in normal (fixed-point) notation.

'%g', '%G' : Scan a floating-point number in either normal or exponential notation. '%g' uses lower-case letters and '%G' uses

upper-case.'%x', '%X' : Scan an integer as an unsigned hexadecimal number.

'%o' : Scan an integer as an octal number.

'%s' : Scan a character string. The scan terminates at whitespace.

A null character is stored at the end of the string, which means that the buffer supplied must be at least one character longer than the specified input length.

'%c' : Scan a character (char). No null character is

added.'(space)': Space scans for whitespace characters.

'%lf' : Scan as a double floating-point number.

'%Lf' : Scan as a long double floating-point number.

附:MSDN中例子

Example

/* FSCANF.C: This program writes formatted

* data to a file. It then uses fscanf to

* read the various data back from the file.

*/

#include

FILE *stream;

void 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' );

/* Set pointer to beginning of file: */

fseek( stream, 0L, SEEK_SET );

/* Read data back from file: */

fscanf( stream, "%s", s );

fscanf( stream, "%ld", &l );

fscanf( stream, "%f", &fp );

fscanf( stream, "%c", &c );

/* Output data read: */

printf( "%s\n", s );

printf( "%ld\n", l );

相关文档
最新文档