概述
I/O函数:
字符i/o
int printf( const char *format, ... );
int scanf( const char*format, ... );
控制符如下:%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//读入等量于int的n个字符%u an unsigned integer%[] a set of characters%% a percent signscanf()会将输入的数据根据参数format字符串来转换并格式化数据。Scanf()格式转换的一般形式如下%[*][size][l][h]type以中括号括起来的参数为选择性参数,而%与type则是必要的。*代表该对应的参数数据忽略不保存。size为允许参数输入的数据长度。[Page]l输入的数据数值以long int或double型保存。h输入的数据数值以short int型保存。[]读取数据但只允许括号内的字符。出现其他字符则终止。如[a-z]。[^]读取数据但不允许中括号的^符号后的字符出现,如[^0-9].返回值成功则返回参数数目,失败则返回-1,错误原因存于errno中。
Getchar putchar:
#include
int getchar( void );#includeint putchar( int c );与scanf(“%s”,&c),printf(“%s”,c),同 效率更高Getc putc:int getc( FILE* fp );int putc( int c,FILE* fp );例:fp = fopen( "file", "r" );if( fp != NULL ) {while( (c = getc( fp )) != EOF ) {putchar(c);}fclose( fp );fputc fgetc:int fgetc( FILE* fp );int fputc( int c,FILE* fp );用法同于putc和getcGets fgets puts fputs:char *gets( char *buf );
bufA buffer where the function can store the string.You should use fgets() instead of gets(); gets() happily overflows the buf array if a newline character isn't read from stdin before the end of the array is reached读取字符直到遇到换行符,读取换行符并丢弃其后加‘o’可能会由于buf不够大导致无法预知的后果应使用fgets替代#includechar* fgets( char* buf, size_t n,FILE* fp );
Arguments:bufA pointer to a buffer in whichfgets()can store the characters that it reads.nThe maximum number of characters to read.fpThe stream from which to read the characters.由n-1和换行符之一首先被满足者决定读入结束,此函数读入换行符并报留在字符串中。当从标准输入读入时fp为stdin。#includeint puts( const char *buf );Arguments:bufA pointer to the zero-terminated string that you want to write.Puts会在字符串末尾自动加上换行符#includeint fputs( const char* buf,FILE* fp );
Arguments:
buf
The string you want to write.
fp
The stream you want to write the string to.Fputs不会自动在字符串尾添加换行符。Sscanf fscanf fprintf:#include
int sscanf( const char *buffer, const char *format, ... );函数用法同scanf,只是该函数的数据是从buffer中读入的
sscanf( " February 0025 1999", "%s %s %d %d", weekday, month, &day, &year );对weekday赋值Thursday...#include
int fscanf( FILE *fp, const char *format, ... );函数类似上函数,只是该函数用于文件操作
fp The stream that you want to read fromint fprintf( FILE* fp,const char* format,... );
fp
The stream to which you want to send the output.
#includeint fread( void *buffer, size_t size, size_t num, FILE *stream );读入文件中的数据到buffer,总共大小为num,size表明读入类型的字节大小,返回值为读入的字节数#include
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );函数将buffer中的内容写入文件中,总共写入cout个size大小的数据,返回写入数据大小的字节数#include
int feof( FILE *stream );如果文件没有读到末尾,返回0,否则返回非0#include
int ferror( FILE *stream );若文件没有错误发生,返回0,否则返回非0#include
void perror( const char *str );打印字符串str和errno相关的错误信息#include
void clearerr( FILE *stream );重新设置stream的错误标识和EOF指示器(错误标识不会自动清除,除非调用clearerr, fseek, fsetpos, or rewind等函数)#include
int fclose( FILE *stream );函数关闭stream文件,释放所有和stream相关的内存资源#include
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
int fgetpos( FILE *stream, fpos_t *position );函数将给定文件的指针存入position变量中,函数成功返回0,否则返回非0fpos_t类型:long integer, __int64, or structure, depending on the target platform#include
int fsetpos( FILE *stream, const fpos_t *position );函数用于设定文件指针,其他规则同fgetpos函数#include
FILE *freopen( const char *fname, const char *mode, FILE *stream );函数重新定向stream的文件流到指定文件的文件流,mode用于指定文件的访问方式函数返回NULL值如果出错,否则返回新文件的文件指针注:可用该函数打开一个文件,并一stdout,stdin做参数,此时可以用在控制台上的函数操作文件但是有一个问题需要解决,怎样把stdout,stdin的指针重新弄回来,以使控制台的输入输出还可用因为该函数执行后会将原来的文件流(stream)指针关闭。在VC中可以通过结合dup和fdopen函数来实现但是在C语言函数库中还不知道用什么函数可以去实现#include
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
long ftell( FILE *stream );函数返回指定文件的当前指针的位置,若出错返回-1#include
int remove( const char *fname );函数关闭fname名字所指定文件流,返回0为成功执行函数,非0为失败#include
int rename( const char *oldfname, const char *newfname );函数更改文件的名字,返回0为成功,非0为失败#include
void rewind( FILE *stream );函数将指定文件的指针移动到文件的开始处,并清除文件的EOF标识#include
void setbuf( FILE *stream, char *buffer );函数设置文件的缓存区buffer,若buffer为NULL值,则文件写入没有缓冲#include
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
FILE *tmpfile( void );函数打开一个临时文件并返回这个临时文件的指针,失败则返回NULL#include
char *tmpnam( char *name );函数创建一个临时文件的文件名,保存在name中#include
int ungetc( int ch, FILE *stream );函数将ch字符放回stream文件中
最后
以上就是年轻钢笔为你收集整理的c语言中printf("%%a",a);,c 语言标准I/O库复习的全部内容,希望文章能够帮你解决c语言中printf("%%a",a);,c 语言标准I/O库复习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复