紧急求助:文件读写相关,在线等

xhz1234 2014-01-15 05:00:29
代码的目的是判断/tmp目录下有没有host_info_p_phy文件,如果有,则将文件中的数字读取保存到一个整型变量中;如果不存在该文件,则创建它,并将123写入该文件,之后再读取该文件,并将文件中的数组读取保存到一个整型变量中。
测试发现,写入文件正常,读取文件不正确。请各高手帮忙看看,很着急,在线等待........

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
FILE *fp = NULL;
char cmd[256] = "\0";
int ret = 0;
int tmp = 0;
if((fp=fopen("/tmp/host_info_p_phy","r"))==NULL)
{
printf("File /tmp/host_info_p_phy not exist!\n");
printf("let's make it!\n");
snprintf(cmd,256,"touch /tmp/host_info_p_phy");
system(cmd);
if((fp=fopen("/tmp/host_info_p_phy","w"))==NULL)
{
printf("Error,no file!\n");
return -1;
}
fprintf(fp,"%d",tmp);
fflush(fp);
}
printf("Position:%d\n", ftell(fp));
rewind(fp);
printf("Position:%d\n", ftell(fp));
fread(&ret,sizeof(int),1,fp);
printf("ret is : %d\n", ret);
fscanf(fp,"%d", &ret);
printf("ret is : %d\n", ret);
fclose(fp);
return 0;
}
...全文
194 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ForestDB 2014-01-16
  • 打赏
  • 举报
回复
#include <stdlib.h> #include <stdio.h> #include <string.h> int main() { FILE *fp = NULL; char cmd[256] = "\0"; int ret = 0; int tmp = 0; if((fp=fopen("/tmp/host_info_p_phy","r"))==NULL) { printf("File /tmp/host_info_p_phy not exist!\n"); printf("let's make it!\n"); snprintf(cmd,256,"touch /tmp/host_info_p_phy"); system(cmd); if((fp=fopen("/tmp/host_info_p_phy","w"))==NULL) { printf("Error,no file!\n"); return -1; } fprintf(fp,"%d",tmp); fflush(fp); // 这里应该关闭fp } // 这里应该以读的方式打开文件 printf("Position:%d\n", ftell(fp)); rewind(fp); printf("Position:%d\n", ftell(fp)); fread(&ret,sizeof(int),1,fp); printf("ret is : %d\n", ret); fscanf(fp,"%d", &ret); printf("ret is : %d\n", ret); fclose(fp); return 0; }
赵4老师 2014-01-16
  • 打赏
  • 举报
回复
When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. 推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。 不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了
hellocondition 2014-01-16
  • 打赏
  • 举报
回复
引用 6 楼 xhz1234 的回复:
已解决,是4楼说的是一个原因。。。主要原因是: 开始/tmp目录下没有host_info_p_phy文件。进入第一个if的语句块中,执行 if((fp=fopen("/tmp/host_info_p_phy","w"))==NULL) ,将123写入文件。之后,从该文件中进行读取数据,此时文件指针fp的性质是“w”,而不是“r”。因此,程序执行第一次时,文件写入成功,但读取错误。
没有注意到楼主的打开文件方式,读写操作应该使用w+模式就可以了。
xhz1234 2014-01-15
  • 打赏
  • 举报
回复
已解决,是4楼说的是一个原因。。。主要原因是: 开始/tmp目录下没有host_info_p_phy文件。进入第一个if的语句块中,执行 if((fp=fopen("/tmp/host_info_p_phy","w"))==NULL) ,将123写入文件。之后,从该文件中进行读取数据,此时文件指针fp的性质是“w”,而不是“r”。因此,程序执行第一次时,文件写入成功,但读取错误。
max_min_ 2014-01-15
  • 打赏
  • 举报
回复
引用 3 楼 xhz1234 的回复:
文件写入正确,从文件中读取不正确
检查下读写函数的返回值!确认下读写数据的时候操作是否正确!
hellocondition 2014-01-15
  • 打赏
  • 举报
回复
不知道lz是哪次读取不对,lz只往文件里写了一个值,而读取了两次,第二次读的时候文件的指针已经指向文件结束了吧。
xhz1234 2014-01-15
  • 打赏
  • 举报
回复
文件写入正确,从文件中读取不正确
zhuobattle 2014-01-15
  • 打赏
  • 举报
回复
不好意思,没看到你的rewind,那么现在具体出了什么问题?
zhuobattle 2014-01-15
  • 打赏
  • 举报
回复
你写完如果马上用同一个fp的话,应该要fseek一下回到文件头

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧