文件操作问题!写了个记时得程序,咋就不行呢?

lockhall 2007-01-05 09:31:31
#include <stdio.h>
#include <time.h>
#include <windows.h>

struct timer
{
int hour;
int minite;
int second;
}t;

int main()
{
FILE *fp;
if(NULL == (fopen("f:\\timeRed.txt","r")))
{
printf("Get the file Failed!");
exit(1);
}
fread(&t,sizeof(struct timer),1,fp);
while(1)
{
rewind(fp);
Sleep(1000);
fread(&t,sizeof(struct timer),1,fp);
if(t.second == 59)
{
t.minite = t.minite+1;
if(t.minite == 60)
{
t.hour = t.hour+1;
}
t.second = 0;
}
else
t.second++;
printf("%d %d %d",t.hour,t.minite,t.second);
fwrite(&t,sizeof(struct timer),1,fp);
fclose(fp);
}
}

在Vc下运行就出现ms内存错误问题~~~高手指点下!
...全文
217 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
IMGGTOO 2007-01-05
  • 打赏
  • 举报
回复
你的文件指针: FILE *fp; 根本就没有初始化,怎么能在 fwrite()里面用呢?
FILE *fp;
if(NULL == (fopen("f:\\timeRed.txt","r")))

改为:

FILE *fp = fopen("f:\\timeRed.txt","r")
if(NULL == fp)
{
...
}

再试试看!
lockhall 2007-01-05
  • 打赏
  • 举报
回复
操作文件是预先创建了.但为空.

这样不行?

t是定义了的...

怎么回事呢?呵呵
jixingzhong 2007-01-05
  • 打赏
  • 举报
回复
操作的文件中预先有了 格式正确的内容了么 ?

先定义一个 结构体变量 t,
用 fwrite(&t,sizeof(struct timer),1,fp);
生成文件内容 ....

69,371

社区成员

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

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