Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1885811
  • 博文数量: 496
  • 博客积分: 12043
  • 博客等级: 上将
  • 技术积分: 4778
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 14:26
文章分类

全部博文(496)

文章存档

2014年(8)

2013年(4)

2012年(181)

2011年(303)

2010年(3)

分类: C/C++

2011-03-09 14:12:56

函数名: fscanf   

功 能: 从一个流中执行格式化输入  
 
用 法: int fscanf(FILE *stream, char *format,[argument...]);  
 
int fscnf(文件指针,格式字符串,输入列表);  
 
返回值:整型,数值等于[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 );   
                 printf( "%f\n", fp );   
                 printf( "%c\n", c );   
                 fclose( stream );   
            }   
}
阅读(583) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~