本文共 1541 字,大约阅读时间需要 5 分钟。
知识点提炼:
#includeint main(){ FILE *stream;//定义文件指针 long len, curpos; stream=fopen("test.txt","w+");//以可读可写的方式打开文件,并且每次文件都是空的 if(stream == NULL) { printf("file not exist"); return -1; } fseek(stream,0L,SEEK_END); //将文件指针移至文件尾部,使其可以变成追加的方式,即a+ fwrite((char *)"hello!\n",sizeof("hello!\n"),1,stream);//将字符串写入,文件流中 fprintf(stream,"create\n");//以数据流的方式输出至文件中,stderr fprintf(stdout,"create\n");//输出至标准输出 if(stream==NULL) { printf("file setup fail \n");//文件创建失败 return -1; } curpos = ftell(stream);//获取文件指针的位置 //获取当前文件的长度 fseek(stream,0L,SEEK_END); len=ftell(stream); fseek(stream,curpos,SEEK_SET);//将文件指针放置在文件的开头 printf("the size of file is %ld \n",len); fclose(stream); return 0;}
编译并测试:
//编译gcc File_test.c -o test//执行./test
输出:
createthe size of file is 15
转载地址:http://eumvn.baihongyu.com/