|
Turbo C 2.0、Borland C++库函数及用例(34) #include <stdio.h>void showpos(FILE *stream);int main(void) { FILE *stream; fpos_t filepos; /* open a file for update */ stream = fopen("DUMMY.FIL", "w+"); /* save the file pointer position */ fgetpos(stream, &filepos); /* write some data to the file */ fprintf(stream, "This is a test"); /* show the current file position */ 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); } /* close the file */ fclose(stream); return 0; }void showpos(FILE *stream) { fpos_t pos; /* display the current file pointer position of a stream */ fgetpos(stream, &pos); printf("File position: %ld\n", pos); } 函数名: fstat 功 能: 获取打开文件信息 用 法: int fstat(char *handle, struct stat *buff); 程序例:#include <sys\stat.h> #include <stdio.h> #include <time.h>int main(void) { struct stat statbuf; FILE *stream; /* open a file for update */ if ((stream = fopen("DUMMY.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output \ file.\n"); return(1); } fprintf(stream, "This is a test"); fflush(stream); /* get information about the file */ fstat(fileno(stream), &statbuf); fclose(stream); /* display the information returned */ 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", 'A'+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)); return 0; } 函数名: ftell 功 能: 返回当前文件指针
|