首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

文件读写初学者有关问题

2012-11-09 
文件读写菜鸟问题求助C/C++ code#include stdio.h#include malloc.h#include string.hint main(){FI

文件读写菜鸟问题求助

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到头部? 用了一路,最后忘了用?
[解决办法]
探讨

int num = fread(ch,1,len,pFile);//读取文件

读之前不fseek到头部? 用了一路,最后忘了用?

[解决办法]
探讨
int num = fread(ch,1,len,pFile);//读取文件

读之前不fseek到头部? 用了一路,最后忘了用?

热点排行