免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1447 | 回复: 7
打印 上一主题 下一主题

文件作数组 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-12-09 21:13 |只看该作者 |倒序浏览
以下程序把文件看作数组.可是为什么无法修改文件数组的值?
#include <stdio.h>
#include <stdlib.h>
void ReadData(FILE* fp)
{   int n,s;
    int i;
        rewind(fp);
    fread(&n,sizeof(int),1,fp);
        for(i=0;i<n;i++){
                fread(&s,sizeof(int),1,fp);
                printf("%d ",s);
        }
}

void main()
{ FILE* fp;
  int i;
  int s;
  int n=256;
  int flag;
  fp=fopen("n.txt","w+b");
  if(fp==0){
          puts("open file error");
          exit(1);
  }
  fwrite(&n,sizeof(int),1,fp);/*输出数组大小*/
  for(i=0;i<256;i++)
          fwrite(&i,sizeof(int),1,fp);

  rewind(fp);
  fread(&n,sizeof(int),1,fp);/*读入数组大小*/
  printf("%d ",n);
  /*修改数据*/
  for(i=0;i<n;i++){
          s=i+1;//对应位加1.
          flag=fwrite(&s,sizeof(int),1,fp);//flag=1,可是在读入数据时
                                            //值还是不变,难道还要做其他
  }                                                              //操作?
   ReadData(fp);
       
}

论坛徽章:
0
2 [报告]
发表于 2005-12-09 21:46 |只看该作者
读写之间貌似要加fseek

论坛徽章:
0
3 [报告]
发表于 2005-12-09 21:55 |只看该作者
不用也可以呀。

论坛徽章:
0
4 [报告]
发表于 2005-12-09 22:32 |只看该作者
哎。。。
在/*修改数据*/下面加一句
fseek(fp, sizeof(n), SEEK_SET);
再试

论坛徽章:
0
5 [报告]
发表于 2005-12-10 15:02 |只看该作者
大家帮帮忙呀,偶实在是找不出错,先行谢过!

论坛徽章:
0
6 [报告]
发表于 2005-12-10 16:29 |只看该作者
问题已经解决,随机写必须用fseek,不能用文件指针自动下移。

#include <stdio.h>
#include <stdlib.h>
void ReadData(FILE* fp)
{   int n,s;
    int i;
        rewind(fp);
    fread(&n,sizeof(int),1,fp);
        for(i=0;i<n;i++){
                fread(&s,sizeof(int),1,fp);
                printf("%d ",s);
        }
}

void main()
{ FILE* fp;
  int i;
  int s;
  int n=256;
  int flag;
  fp=fopen("n.txt","w+b");
  if(fp==0){
          puts("open file error");
          exit(1);
  }
  fwrite(&n,sizeof(int),1,fp);/*输出数组大小*/
  for(i=0;i<256;i++)
          fwrite(&i,sizeof(int),1,fp);
  ReadData(fp);
  putchar('\n');

  rewind(fp);
  fread(&n,sizeof(int),1,fp);/*读入数组大小*/
   /*修改数据*/
  for(i=0;i<n;i++){
              fseek(fp,4*(i+1),0);//随机读写必须加上这一行,至于具体原因未知。
          s=i+1;//对应位加1.
                  //printf("%d ", ftell(fp) );
          flag=fwrite(&s,4,1,fp);//flag=1,可是在读入数据时
                                            //值还是不变,难道还要做其他
  }
  puts("");
  ReadData(fp);
        
}

论坛徽章:
0
7 [报告]
发表于 2005-12-10 17:05 |只看该作者
excerpt from C99

7.19.5.3 The fopen function

6 When a file is opened with update mode ('+' as the second or third character in the
above list of mode argument values), both input and output may be performed on the
associated stream. However, output shall not be directly followed by input without an
intervening call to the fflush function or to a file positioning function (fseek,
fsetpos, or rewind), and input shall not be directly followed by output without an
intervening call to a file positioning function, unless the input operation encounters endof-
file. Opening (or creating) a text file with update mode may instead open (or create) a
binary stream in some implementations.

论坛徽章:
0
8 [报告]
发表于 2005-12-10 17:21 |只看该作者
呵呵,多谢你一直帮我觖决这个问题!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP