当前位置:首页 > 网络编程 > 软件语言 > C语言 > c语言IO库函数学习

c语言IO库函数学习

点击次数:35 次 发布日期:2008-12-01 11:57:06 作者:源代码网
源代码网推荐

    看下面的代码,通过代码来学习C语言IO库函数

/*
  #include <stdio.h>
  int sprintf( char *buffer, const char *format, ... );

  #include <stdio.h>
  int printf( const char *format, ... );
  已有介绍

  #include <stdio.h>
  int fprintf( FILE *stream, const char *format, ... );
  类似上两函数,只是该函数用于文件操作

  #include <stdio.h>
  int scanf( const char *format, ... );
    函数以给定字符串的格式从标准输入流中读入数据(stdin)
    将数据保存在给定参数中,它忽略空格(tab,spaces,etc)
    跳过不符合的字符,返回被赋值的变量数,若出错返回EOF
    控制符如下:
        %c a single character  
        %d a decimal integer  
        %i an integer  
        %e, %f, %g a floating-point number  
        %o an octal number  
        %s a string  
        %x a hexadecimal number  
        %p a pointer  
        %n an integer equal to the number of characters read so far  
        %u an unsigned integer  
        %[] a set of characters  
        %% a percent sign  
    scanf()会将输入的数据根据参数format字符串来转换并格式化数据。Scanf()格式转换的一般形式如下
    %[*][size][l][h]type
    以中括号括起来的参数为选择性参数,而%与type则是必要的。
    * 代表该对应的参数数据忽略不保存。
    size 为允许参数输入的数据长度。 [Page]
    l 输入的数据数值以long int 或double型保存。
    h 输入的数据数值以short int 型保存。
    [] 读取数据但只允许括号内的字符。出现其他字符则终止。如[a-z]。
    [^] 读取数据但不允许中括号的^符号后的字符出现,如[^0-9]. 
    返回值  成功则返回参数数目,失败则返回-1,错误原因存于errno中。

  #include <stdio.h>
  int sscanf( const char *buffer, const char *format, ... );
    函数用法同scanf,只是该函数的数据是从buffer中读入的

  #include <stdio.h>
  int fscanf( FILE *stream, const char *format, ... );
  函数类似上函数,只是该函数用于文件操作

  #include <stdio.h>
  char *gets( char *str );
    从标准输入(stdin)中读入一行数据或是遇到错误,并且在最后加入’ ’值

  #include <stdio.h>
  char *fgets( char *str, int num, FILE *stream );
    类似上函数,该函数用与文件操作,返回读到的字符串,如果有错返回EOF,参数num为最多能读的数据(num-1,最后一个为null值)
    若连续用fgets函数读文件中的数据,则应用fseek函数移动文件指针到下一行初(fseek(file, 2, SEEK_CUR)
    在windows中,换行为两个字符,即回车换行

  #include <stdio.h>
  int getchar( void );
    从stdin中读入一个字符返回,注意返回为int型

  #include <stdio.h>
  int fgetc( FILE *stream );
    返回文件流中的下一个字符,返回EOF如果读到文件末尾或发生错误

  #include <stdio.h>
  int getc( FILE *stream );
    同上一个函数

  #include <stdio.h>
  int putchar( int ch );
    在stdin中写入一个字符,返回EOF如果出错,否则返回写入的字符

  #include <stdio.h>
  int putc( int ch, FILE *stream ); 源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华