C语言IO库函数

#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 );
在文件流中写入一个字符,返回EOF如果出错,否则返回写入的字符
#include <stdio.h>
int puts( char *str );
在文件中写入str字符,如果出错返回EOF值,否则返回非负数值 [Page]
#include <stdio.h>
int fread( void *buffer, size_t size, size_t num, FILE *stream );
读入文件中的数据到buffer,总共大小为num,size表明读入类型的字节大小,返回值为读入的字节数
#include <stdio.h>
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
函数将buffer中的内容写入文件中,总共写入cout个size大小的数据,返回写入数据大小的字节数
#include <stdio.h>
int feof( FILE *stream );
如果文件没有读到末尾,返回0,否则返回非0
#include <stdio.h>
int ferror( FILE *stream );
若文件没有错误发生,返回0,否则返回非0
#include <stdio.h>
void perror( const char *str );
打印字符串str和errno相关的错误信息
#include <stdio.h>
void clearerr( FILE *stream );
重新设置stream的错误标识和EOF指示器(错误标识不会自动清除,除非调用clearerr, fseek, fsetpos, or rewind 等函数)
#include <stdio.h>
int fclose( FILE *stream );
函数关闭stream文件,释放所有和stream相关的内存资源
#include <stdio.h>
FILE *fopen( const char *fname, const char *mode );
函数fname指定的文件,若文件不存在,则新建该文件,mode表示打开文件的模式,若出错,返回NULL
/"r/" Open a text file for reading
/"w/" Create a text file for writing
/"a/" Append to a text file
/"rb/" Open a binary file for reading
/"wb/" Create a binary file for writing
/"ab/" Append to a binary file
/"r /" Open a text file for read/write
/"w /" Create a text file for read/write [Page]
/"a /" Open a text file for read/write
/"rb /" Open a binary file for read/write
/"wb /" Create a binary file for read/write
/"ab /" Open a binary file for read/write
#include <stdio.h>
int fgetpos( FILE *stream, fpos_t *position );
函数将给定文件的指针存入position变量中,函数成功返回0,否则返回非0
fpos_t类型:long integer, __int64, or structure, depending on the target platform
#include <stdio.h>
int fsetpos( FILE *stream, const fpos_t *position );
函数用于设定文件指针,其他规则同fgetpos函数
#include <stdio.h>
FILE *freopen( const char *fname, const char *mode, FILE *stream );
函数重新定向stream的文件流到指定文件的文件流,mode用于指定文件的访问方式
函数返回NULL值如果出错,否则返回新文件的文件指针
注:可用该函数打开一个文件,并一stdout,stdin做参数,此时可以用在控制台上的函数操作文件
但是有一个问题需要解决,怎样把stdout,stdin的指针重新弄回来,以使控制台的输入输出还可用
因为该函数执行后会将原来的文件流(stream)指针关闭。在VC中可以通过结合dup和fdopen函数来实现
但是在C语言函数库中还不知道用什么函数可以去实现
#include <stdio.h>
int fseek( FILE *stream, long offset, int origin );
函数设置文件流stream指针到给定的偏移量,偏移量与origin相对而言
origin可取值:
SEEK_SET Seek from the start of the file
SEEK_CUR Seek from the current location
SEEK_END Seek from the end of the file
函数返回0为成功,非0为失败,该函数可以清除EOF标记 [Page]
#include <stdio.h>
long ftell( FILE *stream );
函数返回指定文件的当前指针的位置,若出错返回-1
#include <stdio.h>
int remove( const char *fname );
函数关闭fname名字所指定文件流,返回0为成功执行函数,非0为失败
#include <stdio.h>
int rename( const char *oldfname, const char *newfname );
函数更改文件的名字,返回0为成功,非0为失败
#include <stdio.h>
void rewind( FILE *stream );
函数将指定文件的指针移动到文件的开始处,并清除文件的EOF标识
#include <stdio.h>
void setbuf( FILE *stream, char *buffer );
函数设置文件的缓存区buffer,若buffer为NULL值,则文件写入没有缓冲
#include <stdio.h>
int setvbuf( FILE *stream, char *buffer, int mode, size_t size );
函数用特定的模式设置文件的缓冲区及大小
mode可取值:
_IOFBF, which indicates full buffering
_IOLBF, which means line buffering
_IONBF, which means no buffering
#include <stdio.h>
FILE *tmpfile( void );
函数打开一个临时文件并返回这个临时文件的指针,失败则返回NULL
#include <stdio.h>
char *tmpnam( char *name );
函数创建一个临时文件的文件名,保存在name中
#include <stdio.h>
int ungetc( int ch, FILE *stream );
函数将ch字符放回stream文件中
#include <stdarg.h>
#include <stdio.h>
int vprintf( char *format, va_list arg_ptr );
int vfprintf( FILE *stream, const char *format, va_list arg_ptr );
int vsprintf( char *buffer, char *format, va_list arg_ptr ); [Page]
These functions are very much like printf(), fprintf(), and sprintf().
The difference is that the argument list is a pointer to a list of arguments.
va_list is defined in stdarg.h, and is also used by (Other Standard C Functions)
va_arg(). For example:

void error( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
fprintf( stderr, /"Error: /" );
vfprintf( stderr, fmt, args );
fprintf( stderr, /"/n/" );
va_end( args );
exit( 1 );
}
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**//*void main()
{
char setTest[10];
scanf(/"%[a-z]/", setTest);
printf(/"%s /", setTest);
}*/
/**//*void main( void )
{
int c;
//Create an error by writing to standard input.
putc( ’c’, stdin );
if( ferror( stdin ) )
{
perror( /"Write error/" );
clearerr( stdin );
}

// See if read causes an error.
printf( /"Will input cause an error? /" );
c = getc( stdin );
if( ferror( stdin ) )
{
perror( /"Read error/" );
clearerr( stdin );
}
}*/
/**//*void main()
{
char read[100];
FILE *orig_stdout = (FILE*)malloc(sizeof(FILE)); [Page]
memcpy(orig_stdout, stdout, sizeof(FILE));
FILE *filew = freopen(/"test.txt/", /"w/", stdout);
//freopen(/"test.txt/", /"w/", stdout);
if(filew == stdout)
printf(/"equal/");
printf(/"We can write datum in file test.txt use printf function with the use of freopen/");
fclose(filew);
/* memcpy(stdout, orig_stdout, sizeof(FILE));
FILE *filer = freopen(/"test.txt/", /"r/", stdin);
//freopen(/"test.txt/", /"r/", stdin);
gets(read);
fclose(filer);
printf(/"%s/", read);
printf(/"%c/", read[0]);
printf(/"jd;salkjf/");
*/
// rename(/"test.txt/", /"newname.txt/");
/

http://humanbeng.blog.163.com/blog/static/9593240120107241134477/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值