fwrite为何调用失败,应如何修改?

nimao1984 2007-10-25 08:00:42
#include <stdio.h>

struct communication
{
int bilseqno[2];
char recordtype;
char recordid;
char naocn;
char cn[20];
}stu;

int main()
{
FILE *fp,*fp1;
if((fp = fopen("d:\\111.txt","r")) == NULL)
{
printf("cannot open the file\n");
exit(0);
}
if (fseek(fp,123L,0))//移动失败,返回非0
{
printf("Seek fail!");
exit(0);
}
if(fread(&stu, sizeof(struct communication), 1, fp) != 1)
{
printf("error\n");
}
if((fp1 = fopen("nidan.txt","w")) == NULL)
{
printf("cannot open the file\n");
exit(0) ;
}
if( fwrite(&stu, sizeof(struct communication), 1, fp1)!=1);
printf("error2\n");
fclose(fp);
fclose(fp1);
}


...全文
412 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huiyamin428 2007-10-26
  • 打赏
  • 举报
回复
// 文件操作.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

struct communication
{
int bilseqno[2];
char recordtype;
char recordid;
char naocn;
char cn[20];
}stu ,stu1;

int main()
{

cout<<"输入数据:"<<endl;
cin>>stu.bilseqno[0];
cin>>stu.bilseqno[1];
cin>>stu.recordtype;
cin>>stu.recordid;
cin>>stu.naocn;
cin>>stu.cn;

cout<<"输出数据:"<<endl;
cout<<stu.bilseqno[0]<<endl;
cout<<stu.bilseqno[1]<<endl;
cout<<stu.recordtype<<endl;
cout<<stu.recordid<<endl;
cout<<stu.naocn<<endl;
cout<<stu.cn<<endl;

FILE *fp,*fp1;
if((fp = fopen("c:\\Temp\\11.txt","w")) == NULL)
{
printf("cannot open the file\n");
exit(1);
}
if (fseek(fp,123L,0)) //移动失败,返回非0
{
printf("Seek fail!");
exit(1);
}


int numwritten = fwrite(&stu, sizeof(struct communication), 1, fp);
printf( "Wrote %d items\n", numwritten );

fclose(fp);

if((fp = fopen("c:\\Temp\\11.txt","r")) == NULL)
{
printf("cannot open the file\n");
exit(1);
}


if(fread(&stu, sizeof(struct communication), 1, fp) != 1)
{
printf("读文件失败!\n");
}

fclose(fp);


cout<<"输入数据:"<<endl;
cin>>stu.bilseqno[0];
cin>>stu.bilseqno[1];
cin>>stu.recordtype;
cin>>stu.recordid;
cin>>stu.naocn;
cin>>stu.cn;


cout<<"输出数据:"<<endl;
cout<<stu.bilseqno[0]<<endl;
cout<<stu.bilseqno[1]<<endl;
cout<<stu.recordtype<<endl;
cout<<stu.recordid<<endl;
cout<<stu.naocn<<endl;
cout<<stu.cn<<endl;

if((fp1 = fopen("c:\\Temp\\12.txt","w")) == NULL)
{
printf("cannot open the file\n");
exit(1) ;
}


// if(fwrite(&stu, sizeof(struct communication), 1, fp1) !=1);
// printf("写文件失败!\n");

int numwritten1 = fwrite(&stu, sizeof(struct communication), 1, fp1);
printf( "Wrote %d items\n", numwritten1 );
fclose(fp1);
return 0;
}


运行结果:
输入数据 1 2 a b c baishijun
输出数据
1
2
a
b
c
baishijun
Wrote 1 items


运行结果:
输入数据 3 4 e f g huiyamin
输出数据
3
4
e
f
g
huiyamin
Wrote 1 items
press any key to continue

这个过程中写是成功了,只是不知道为什么fwrite的numwritten1显示是1,但是为什么在if条件中条件是为真的,我就纳闷了,还没有查找出原因来,调试通过了,文件中也有写进去的信息。
Arthur_ 2007-10-26
  • 打赏
  • 举报
回复
if( fwrite(&stu, sizeof(struct communication), 1, fp1)!=1);
多个分号
jixingzhong 2007-10-26
  • 打赏
  • 举报
回复
fp = fopen("c:\\Temp\\11.txt","w")
==》
fp = fopen("c:\\Temp\\11.txt","wb")


其他类同修改
ckt 2007-10-25
  • 打赏
  • 举报
回复
fwrite(&stu, sizeof(struct communication), 1, fp1)!=1

写之前还要判断下stu是否读取到了数据
独孤过儿 2007-10-25
  • 打赏
  • 举报
回复
检查一下你 fopen 的打开模式是否允许写

69,382

社区成员

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

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