/**//* #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 为允许参数输入的数据长度。 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值,否则返回非负数值 #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 "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标记 #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 ); 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)); 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"); //} /**//* SETBUF.C: This program first opens files named DATA1 and * DATA2. Then it uses setbuf to give DATA1 a user-assigned * buffer and to change DATA2 so that it has no buffer. */ /**//*void main( void ) { char buf[BUFSIZ]; FILE *stream1, *stream2; if( ((stream1 = fopen( "data1", "a" )) != NULL) && ((stream2 = fopen( "data2", "a" )) != NULL) ) { // "stream1" uses user-assigned buffer: setbuf( stream1, buf ); //set the buffer so the next datum write to the file will be written to buf first //the datum is written to the file after use fflush of fclose function fprintf(stream1, "append the string"); printf( "stream1 set to user-defined buffer at: %Fp ", buf ); // "stream2" is unbuffered setbuf( stream2, NULL ); printf( "stream2 buffering disabled " ); fprintf(stream2, "string written in the stream2 file use no buffer, that is written directly"); _fcloseall(); } } */ /**//* SSCANF.C: This program uses sscanf to read data items * from a string named tokenstring, then displays them. */ void main( void ) ...{ char tokenstring[] ="15 12 14..."; char s[81]; char c; int i; float fp; //FILE *tempfile = tmpfile(); char tempname[10]; tmpnam(tempname); // Input various data from tokenstring: // note:all read from the begin sscanf( tokenstring, "%s", s ); sscanf( tokenstring, "%c", &c ); sscanf( tokenstring, "%d", &i ); sscanf( tokenstring, "%f", &fp ); // Output the data read printf( "String = %s ", s ); printf( "Character = %c ", c ); printf( "Integer: = %d ", i ); printf( "Real: = %f ", fp ); FILE *file = fopen("ungetc.txt", "w+"); fputs("put the string in file", file); //fflush(file); rewind(file); //fseek(file,0,SEEK_SET); char ch; ch = fgetc(file); ch = fgetc(file); ch = fgetc(file); ungetc(ch, file); fclose(file); }