下载的文档为原格式,如果无法下载或内容有问题,请联系客服QQ:1920494503

stdio.h 头文件提供的常用函数解析.txt

stdio.h 头文件提供的常用函数解析 @函数名称: fseek 函数原型: int fseek(FILE * fp,long offset,int base); 函数功能: 将fp所指文件的位置指针移到以base所指位置为基准,以offset为位移量的位置 函数返回: 返回当前位置,否则返回-1 参数说明: fp-文件指针 offset-相对于origin规定的偏移位置量 origin-指针移动的起始位置,可设置为以下三种情况: SEEK_SET 文件开始位置 0 SEEK_CUR 文件当前位置 1 SEEK_END 文件结束位置 2 所属文件: <stdio.h> #include <stdio.h> long filesize(FILE *stream); int main() { FILE *stream; stream=fopen("MYFILE.TXT","w+"); fprintf(stream,"This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes",filesize(stream)); fclose(stream); return 0; } long filesize(FILE *stream) { long curpos,length; curpos=ftell(stream); fseek(stream,0L,SEEK_END); length=ftell(stream); fseek(stream,curpos,SEEK_SET); return length; } @函数名称: ftell 函数原型: long ftell(FILE * fp); 函数功能: 得到文件位置指示器的数值 函数返回: fp指向的文件中的读写位置 参数说明: 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *stream; stream=fopen("MYFILE.TXT","w+"); fprintf(stream,"This is a test"); printf("The file pointer is at byte %ld",ftell(stream)); fclose(stream); return 0; } @函数名称: fopen 函数原型: FILE *fopen(char * filename,char * mode); 函数功能: 以mode指定的方式打开名为filename的文件 函数返回: 成功,返回一个文件指针(文件信息区的起始地址),否则返回0 参数说明: filename-文件名称,mode-打开模式: r 只读方式打开一个文本文件 rb 只读方式打开一个二进制文件 w 只写方式打开一个文本文件 wb 只写方式打开一个二进制文件 a 追加方式打开一个文本文件 ab 追加方式打开一个二进制文件 r+ 可读可写方式打开一个文本文件 rb+ 可读可写方式打开一个二进制文件 w+ 可读可写方式创建一个文本文件 wb+ 可读可写方式生成一个二进制文件 a+ 可读可写追加方式打开一个文本文件 ab+ 可读可写方式追加一个二进制文件 所属文件: <stdio.h> #include <stdlib.h> #include <stdio.h> #include <dir.h> int main() { char *s; char drive[MAXDRIVE]; char dir[MAXDIR]; char file[MAXFILE]; char ext[MAXEXT]; int flags; s=getenv("COMSPEC"); flags=fnsplit(s,drive,dir,file,ext);

第1页

printf("Command processor info:"); if(flags&DRIVE) printf("\tdrive: %s",drive); if(flags&DIRECTORY) printf("\tdirectory: %s",dir); if(flags&FILENAME) printf("\tfile: %s",file); if(flags&EXTENSION) printf("\textension: %s",ext); return 0; } @函数名称: fclose 函数原型: int fclose(FILE * fp); 函数功能: 关闭fp所指的文件,释放文件缓冲区 函数返回: 0-无错,否则非零 参数说明: 所属文件: <stdio.h> #include <string.h> #include <stdio.h> int main() { FILE *fp; char buf[11]="0123456789"; fp=fopen("test.txt","w"); fwrite(&buf,strlen(buf),1,fp); fclose(fp); return 0; } @函数名称: fflush 函数原型: int fflush(FILE *stream) 函数功能: 清除文件缓冲区,文件以写方式打开时将缓冲区内容写入文件 函数返回: 0-操作成功,非0-操作失败 参数说明: stream-文件指针 所属文件: <stdio.h> #include <string.h> #include <stdio.h> #include <conio.h> #include <io.h> void flush(FILE *stream); int main() { FILE *stream; char msg[]="This is a test"; stream=fopen("DUMMY.FIL","w"); fwrite(msg,strlen(msg),1,stream); clrscr(); printf("Press any key to flush DUMMY.FIL:"); getch(); flush(stream); printf("File was flushed,Press any key to quit:"); getch(); return 0; } void flush(FILE *stream) { int duphandle; fflush(stream); duphandle=dup(fileno(stream)); close(duphandle); } @函数名称: ferror 函数原型: int ferror(FILE *stream) 函数功能: 检测文件操作是否有错误 函数返回: 非0-有错误,0-无错误 参数说明: stream-文件指针 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *stream; stream=fopen("DUMMY.dat","w"); (void) getc(stream); if (ferror(stream)) { printf("Error reading from DUMMY.dat") clearerr(stream); } fclose(stream); return 0; } @函数名称: fileno 函数原型: int fileno(FILE *fp) 函数功能: 得到与fp等价的文件句柄号 函数返回: 文件句柄号 参数说明: fp-由fopen()函数打开的文件指针 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *stream; stream=fopen("file","r"); printf("File number is %d\n",fileno(stream)); fclose(stream); return 0; } @函数名称: freopen 函数原型: FILE *freopen(char *fname,char *mode,FILE *fp) 函数功能: 将一个已打开的fp和一个新的文件名相连接 函数返回: fp值 参数说明: fp-已打开的文件指针 fname-新的文件名称 mode-同函数fopen()中的定义 所属文件: <stdio.h> #include <stdio.h> int main() { if (freopen("OUTPUT.FIL","w",stdout)==NULL)

第2页

fprintf(stderr,"error redirecting stdout"); printf("This will go into a file."); fclose(stdout); return 0; } @函数名称: clearerr 函数原型: void clearerr(FILE * fp); 函数功能: 清除文件指针错误指示器,将文件出错标志清零 函数返回: 参数说明: fp-文件的流指针 所属文件: <stdio.h> #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main() { int gdriver=DETECT,gmode,errorcode; int midx,midy; initgraph(&gdriver,&gmode,); errorcode=graphresult(); if (errorcode!=grOk) { printf("Graphics error: %s",grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } midx=getmaxx()/2; midy=getmaxy()/2; setcolor(getmaxcolor()); settextjustify(CENTER_TEXT,CENTER_TEXT); outtextxy(midx,midy,"press any key to clear the screen:"); getch(); cleardevice(); outtextxy(midx,midy,"press any key to quit:"); getch(); closegraph(); return 0; } @函数名称: fgetc 函数原型: int fgetc(FILE * fp); 函数功能: 从fp所指定的文件中取得下一个字符 函数返回: 返回所得到的字符.若读入出错,返回EOF 参数说明: fp-文件指针 所属文件: <stdio.h> #include <string.h> #include <stdio.h> #include <conio.h> int main() { FILE *stream; char string[]="This is a test"; char ch; stream=fopen("test.txt","w+"); fwrite(string,strlen(string),1,stream); fseek(stream,0,SEEK_SET); do { ch=fgetc(stream); putch(ch); } while (ch!=EOF); fclose(stream); return 0; } @函数名称: fgetchar 函数原型: int fgetchar(void) 函数功能: 等价于fgetc(stdin),也等价于getchar,但仅作为函数实现 函数返回: 返回读出的字符,如文件已到结尾,返回值为EOF 参数说明: fp-文件指针 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *fp; int c; fp = freopen( "file", "r", stdin ); while( (c = getchar()) != EOF ) putchar(c); fclose( fp ); return 0; } @函数名称: fputchar 函数原型: int fputchar(int ch) 函数功能: 在标准输出流(即屏幕上)的当前位置写入一个字符 函数返回: 操作正确时返回写入的字符,错误返回EOF 参数说明: ch-要写入的字符(舍去高位字节) 所属文件: <stdio.h> #include <stdio.h> int main() { char msg[] ="This is a test"; int i=0; while (msg[i]) { fputchar(msg[i]); i++; } return 0; } @函数名称: fgets 函数原型: char fgets(char * buf,int n,FILE * fp); 函数功能: 从fp指向的文件中读取一个长度为(n-1)的字符串,存入起始地址为buf的空间

第3页

函数返回: 返回地址buf,若遇文件结束或出错,返回NULL 函数说明: buf-存放读入的字符数组指针,n-最大允许的读入字符数,fp-文件指针 所属文件: <stdio.h> #include <string.h> #include <stdio.h> int main() { FILE *stream; char string[]="This is a test"; char msg[20]; stream=fopen("test.txt","w+"); fwrite(string,strlen(string),1,stream); fseek(stream,0,SEEK_SET); fgets(msg,strlen(string)+1,stream); printf("%s",msg); fclose(stream); return 0; } @函数名称: feof 函数原型: int feof(FILE * fp); 函数功能: 检查文件是否结束. 函数返回: 遇文件结束符返回非零值,否则返回0 参数说明: fp-文件指针 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *stream; stream=fopen("test.txt","r"); fgetc(stream); if (feof(stream)) printf("We have reached end-of-file"); fclose(stream); return 0; } 函数名称: fputc 函数原型: int fputc(char ch,FILE *fp); 函数功能: 将字符ch输出到fp指向的文件中 函数返回: 成功,则返回该字符;否则返回非0 参数说明: fp-文件指针,ch-要写入的字符(舍去高位字节) 所属文件: <stdio.h> #include <stdio.h> int main() { char msg[]="Hello world"; int i=0; while (msg[i]) { fputc(msg[i],stdout); i++; } return 0; } @函数名称: fputs 函数原型: int fputs(char * str,FILE *fp); 函数功能: 将str指向的字符串输出到fp指向的文件中 函数返回: 成功,则返回0;否则返回非0 参数说明: 所属文件: <stdio.h> #include <stdio.h> int main() { fputs("Hello world",stdout); return 0; } @函数名称: fread 函数原型: int fread(char * pt,unsigned size,unsigned n,FILE * fp); 函数功能: 从fp所指定的文件中读取长度为size的n个数据项,存到pt所指向的内存区 函数返回: 返回所读的数据项个数,如遇文件结束或出错返回0 参数说明: pt-存放读入数据的指针,size-每个数据单位的字节数,n-读入的数据单位个数 所属文件: <stdio.h> #include <string.h> #include <stdio.h> int main() { FILE *stream; char msg[]="this is a test"; char buf[20]; if ((stream=fopen("test.txt","w+"))==NULL) { fprintf(stderr,"Cannot open output file."); return 1; } fwrite(msg,strlen(msg)+1,1,stream); fseek(stream,SEEK_SET,0); fread(buf,strlen(msg)+1,1,stream); printf("%s",buf); fclose(stream); return 0; } @函数名称: fwrite 函数原型: int fwrite(char *ptr,unsigned size,unsigned n,FILE *fp); 函数功能: 把ptr所指向的n*size个字节输出到fp所指向的文件中. 函数返回: 写到fp文件中的数据项个数 参数说明:

第4页

ptr-存放要写入的数据,size-每个数据单位的字节数,n-读入的数据单位个数 所属文件: <stdio.h> #include <stdio.h> struct mystruct{ int i; char ch; }; int main() { FILE *stream; struct mystruct s; if ((stream=fopen("TEST.dat","wb"))==NULL) { fprintf(stderr,"Cannot open output file."); return 1; } s.i=0; s.ch='A'; fwrite(&s,sizeof(s),1,stream); fclose(stream); return 0; } @函数名称: fprintf 函数原型: int fprintf(FILE * fp,char * format,args,...); 函数功能: 把args的值以format指定的格式输出到fp所指定的流式文件中 函数返回: 实际输出的字符数 参数说明: fp-目标文件,format-格式符 所属文件: <stdio.h> #include <stdio.h> int main() { FILE *in,*out; if ((in=fopen("AUTOEXEC.BAT","rt"))==NULL) { fprintf(stderr,"Cannot open input file."); return 1; } if ((out=fopen("AUTOEXEC.BAK","wt"))==NULL) { fprintf(stderr,"Cannot open output file."); return 1; } while(!feof(in)) fputc(fgetc(in),out); fclose(in); fclose(out); return 0; } @函数名称: fscanf 函数原型: int fscanf(FILE * fp,char format,args,...); 函数功能: 从fp所指定的文件中按format给定的格式将数据输送到args所指向的内存单元 函数返回: 已输入的数据个数 参数说明: 所属文件: <stdio.h> #include <stdlib.h> #include <stdio.h> int main() { int i; printf("Input an integer: "); if (fscanf(stdin,"%d",&i)) printf("The integer read was: %i",i); else { fprintf(stderr,"Error reading an integer from stdin."); exit(1); } return 0; } @函数名称: scanf 函数原型: int scanf(char * format,args,...); 函数功能: 从标准输入设备按format指向的格式字符串规定的格式,输入数据给agrs所指向的单元 函数返回: 读入并赋给args的数据个数.遇文件结束返回EOF,出错返回0 参数说明: args-指针 所属文件: <stdio.h> int main() { int a,b,c; scanf("%d%d%d",&a,&b,&c); printf("%d,%d,%d\n",a,b,c); return 0; } @函数名称: printf 函数原型: int printf(char * format,args,...); 函数功能: 按format指向的格式字符串所规定的格式,将输出表列args的值输出到标准输出设备 函数返回: 输出字符的个数.若出错返回负数 参数说明: format-是一个字串,或字符数组的起始地址 所属文件: <stdio.h> #include <stdio.h> int main() { char c='a'; int i=97; printf("%c,%d\n",c,c); printf("%c,%d\n",i,i); return 0; }

第5页
相关文档
本站文档均来自互联网及网友上传分享,本站只负责收集和整理,有侵权可通过QQ客服反馈删除。侵权投诉