#include<string.h>#include<stdio.h>intmain(void){
FILE *fp;char buf[30]="Hello, Huazie 123456789";
fp =fopen("STU.FIL","w");printf("temp.txt is created and opened\n");fwrite(&buf,strlen(buf),1, fp);printf("temp.txt is writed\n");fclose(fp);printf("temp.txt is closed");return0;}
27.3 运行结果
28. fprintf
28.1 函数说明
函数声明
函数功能
int fprintf(FILE *stream, char *format[, argument,...]);
int fscanf(FILE *stream, char *format[,argument...]);
从一个流中执行格式化输入
注意: fscanf 遇到空格和换行时结束。它与 fgets 有区别,fgets 遇到空格不结束。
36.2 演示示例
#include<stdlib.h>#include<stdio.h>intmain(void){int i;printf("Input an integer: ");if(fscanf(stdin,"%d",&i))printf("The integer is: %d\n", i);else{fprintf(stderr,"Error reading an integer from stdin.\n");exit(1);}return0;}
36.3 运行结果
37. fseek
37.1 函数说明
函数声明
函数功能
int fseek(FILE *stream, long offset, int fromwhere);
#include<stdio.h>longfilesize(FILE *stream);intmain(void){
FILE *stream =fopen("temp.txt","w+");fprintf(stream,"This is a test");printf("The size of temp.txt is %ld bytes\n",filesize(stream));fclose(stream);return0;}longfilesize(FILE *stream){long curpos, length;// 文件指针当前位置相对于文件首的偏移字节数
curpos =ftell(stream);// 重定向文件指针到文件尾,偏移量 0fseek(stream,0L,SEEK_END);
length =ftell(stream);return length;}
37.3 运行结果
38. fsetpos
38.1 函数说明
函数声明
函数功能
int fsetpos(FILE *stream, const fpos_t *pos);
将文件指针定位在pos指定的位置上。如果成功返回0,否则返回非0。
38.2 演示示例
#include<stdlib.h>#include<stdio.h>voidshowpos(FILE *stream);intmain(void){
FILE *stream;fpos_t filepos;
stream =fopen("STU.FIL","w+");// 获取当前文件指针的位置fgetpos(stream,&filepos);fprintf(stream,"This is a test");// 展示当前文件指针的位置showpos(stream);/* set a new file position, display it */if(fsetpos(stream,&filepos)==0)showpos(stream);else{fprintf(stderr,"Error setting file pointer.\n");exit(1);}fclose(stream);return0;}voidshowpos(FILE *stream){fpos_t pos;// 展示当前文件指针的位置fgetpos(stream,&pos);printf("File position: %ld\n", pos);}
38.3 运行结果
39. fstat
39.1 函数说明
函数声明
函数功能
int fstat(int handle,struct stat *buf);
由文件描述符获取文件状态
39.2 演示示例
#include<sys\stat.h>#include<stdio.h>#include<time.h>intmain(){structstat statbuf;
FILE *stream;if((stream =fopen("STU.FIL","w+"))==NULL){fprintf(stderr,"Cannot open output file.\n");exit(1);}fprintf(stream,"This is a test");fflush(stream);// get information about the filefstat(fileno(stream),&statbuf);fclose(stream);if(statbuf.st_mode & S_IFCHR)printf("Handle refers to a device.\n");if(statbuf.st_mode & S_IFREG)printf("Handle refers to an ordinary file.\n");if(statbuf.st_mode & S_IREAD)printf("User has read permission on file.\n");if(statbuf.st_mode & S_IWRITE)printf("User has write permission on file.\n");// 不知道为啥,我这里文件的驱动号是空printf("Drive letter of file: %c\n", statbuf.st_dev);printf("Size of file in bytes: %ld\n", statbuf.st_size);printf("Time file last opened: %s\n",ctime(&statbuf.st_ctime));return0;}
39.3 运行结果
40. ftell
40.1 函数说明
函数声明
函数功能
long ftell(FILE *stream);
获取文件指针当前位置相对于文件首的偏移字节数
40.2 演示示例
#include<stdio.h>intmain(void){
FILE *stream =fopen("temp.txt","w+");fprintf(stream,"This is a test");printf("The file pointer is at byte %ld\n",ftell(stream));fclose(stream);return0;}
40.3 运行结果
41. fwrite
41.1 函数说明
函数声明
函数功能
int fwrite(const void *ptr, int size, int nitems, FILE *stream);