文件读写初学者有关问题
文件读写菜鸟问题求助
------解决方案--------------------
int num = fread(ch,1,len,pFile);//读取文件
读之前不fseek到头部? 用了一路,最后忘了用?
------解决方案--------------------
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <malloc.h> #include <string.h> int main() { FILE *pFile = fopen("1.txt","w+"); fwrite("hello,world!",1,strlen("hello,world!"),pFile); fflush(pFile); fseek(pFile,0,SEEK_SET); //文件指针设为起点,写操作覆盖原来的内容 fwrite("欢迎访问",1,strlen("欢迎访问"),pFile); fseek(pFile,0,SEEK_END); //文件指针设为终点 int len = ftell(pFile); //获取文件字节数 char* ch = (char*)malloc(sizeof(char)* (len+1)); //分配内存,多一个字节 memset(ch,0,(len+1)); //清0 int num = fread(ch,1,len,pFile);//读取文件 printf("读出字节:%d\n",num); //为什么num为0?? fclose(pFile); printf("%s",ch); return 0; }
------解决方案--------------------
int num = fread(ch,1,len,pFile);//读取文件
读之前不fseek到头部? 用了一路,最后忘了用?
------解决方案--------------------
------解决方案--------------------