| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 391 人关注过本帖
标题:文件方面的问题
只看楼主 加入收藏
潇湘书生
Rank: 2
等 级:论坛游民
威 望:1
帖 子:23
专家分:89
注 册:2014-12-9
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
文件方面的问题
每当我把数据输进去之后,当我输入最后一个数据回车之后,就会弹出问题("file write error\n");也就是说我的数据写入文件失败对吧,不知该如何解决,求教。


#include<stdio.h>
#include<process.h>
#define size 9
typedef struct tax_st
{
 long left;//区间上限
 long right;//区间下限
 int tax;//税率
 long deduct;//速算扣除数
}TAX_LIST;
void acceptdata(TAX_LIST tax_list[])
{
 int i;
 for(i=0;i<size;i++)
 {
  printf("Please enter data:\n");
  scanf("%ld",&tax_list[size].left);
  scanf("%ld",&tax_list[size].right);
  scanf("%d",&tax_list[size].tax);
  scanf("%ld",&tax_list[size].deduct);
 }
}

int main()
{
 FILE *fp;
 int i;
 TAX_LIST tax_list[size];
 if((fp=fopen("d:\\TAX.din","wb"))==NULL)//打开文件TAX.din
 {
  printf("can not open the file\n");
  exit(1);//打开失败退出
 }
 acceptdata(tax_list);//调用函数从键盘接收数据
 for(i=0;i<size;i++)
 if(fwrite(tax_list,sizeof(TAX_LIST),1,fp)!=1)//将数组tax_list的结构数据写入文件
     printf("file write error\n");
 fclose(fp);
 return 0;
}
搜索更多相关主题的帖子: file write include 如何 
2015-02-02 17:25
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:20 
看到一堆错误,修改后代码如下(红色部分修改):

#include<stdio.h>
#include<process.h>
#define size 9
typedef struct tax_st
{
  long left;//区间上限
  long right;//区间下限
  int tax;//税率
  long deduct;//速算扣除数
}TAX_LIST;
void acceptdata(TAX_LIST tax_list[])
{
  int i;
  for(i=0;i<size;i++)
  {
    printf("Please enter data:\n");
    scanf("%ld",&tax_list[i].left);  //所有数组下标有size改为i
    scanf("%ld",&tax_list[i].right);
    scanf("%d",&tax_list[i].tax);
    scanf("%ld",&tax_list[i].deduct);
  }
}

int main()
{
  FILE *fp;
  TAX_LIST tax_list[size];
  if((fp=fopen("d:\\TAX.din","wb"))==NULL)//打开文件TAX.din
  {
    printf("can not open the file\n");
    exit(1);//打开失败退出
  }
  acceptdata(tax_list);//调用函数从键盘接收数据
  if(fwrite(tax_list,sizeof(tax_list),1,fp)!=1)//大小写敏感,不需要循环,一次性写入结构体数组
     printf("file write error\n");
  fclose(fp);
  return 0;
}

能编个毛线衣吗?
2015-02-02 19:52
潇湘书生
Rank: 2
等 级:论坛游民
威 望:1
帖 子:23
专家分:89
注 册:2014-12-9
收藏
得分:0 
回复 2楼 wmf2014
谢谢,问题顺利解决。
2015-02-02 20:23
快速回复:文件方面的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019889 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved