利用fopen打开文件,无论文件是否存在都显示打开成功,而且文件头和文件尾的位置相等(ftell测试文件首和文件尾都返回0)
C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445#include <stdio.h>#include <stdlib.h>#define FNSIZE 256int main(void){ char filename[FNSIZE]={0}; int psn; char buffer; FILE *fp; fprintf(stdout,"Enter the file name.\n"); gets(filename); if((fp=fopen(filename,"rb"))==NULL) { fprintf(stderr,"Open file:%s failed.\n",filename); exit(1); } printf("filename:%s",filename); fprintf(stdout,"Present position:%d\n",ftell(fp)); fprintf(stdout,"Enter the offset(int) to present position.\n"); while(fscanf(stdin,"%d",&psn)!=0) { if(fseek(fp,psn,SEEK_CUR)==0) { buffer=getc(fp); fprintf(stderr,"Present position:%d\n",ftell(fp)); fseek(fp,0L,SEEK_END); fprintf(stderr,"End position:%d\n",ftell(fp)); while(buffer!='\n' && buffer!=EOF) { putc(buffer,stdout); } } else { fprintf(stderr,"To the end of file.\n"); break; } fprintf(stdout,"*******************************************************\n"); fprintf(stdout,"Enter next position.\n"); } return 0;}
千万里不及你
天涯尽头无女友
相关分类