鱼C论坛

 找回密码
 立即注册
查看: 1374|回复: 2

[已解决]想要存一个二维数组到文件中,再在之后的操作中读出来,但是b[1][1]一直给随机数sh...

[复制链接]
发表于 2020-12-21 17:55:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 空羊羊 于 2020-12-21 18:41 编辑

void main(void){
        int arr[5][5]={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4};
        int brr[5][5];
        FILE *fp;
        if((fp=fopen("a.txt","w"))==NULL){
                printf("Fail to open the file.\n");
        }
        fwrite(arr, 25*sizeof(int), 1, fp);
        fclose(fp);
        if((fp=fopen("a.txt","w"))==NULL){
                printf("Fail to open the file.\n");
        }
        fread(brr, 25*sizeof(int), 1, fp);
        fclose(fp);       
        printf("%d", brr[1][1]);
}
最佳答案
2020-12-21 18:43:13
  1. void main(void)
  2. {
  3.         int arr[5][5]={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4};
  4.         int brr[5][5];
  5.         FILE *fp;
  6.        //修改文件打开方式
  7.         if((fp=fopen("a.txt","wb"))==NULL){
  8.                 printf("Fail to open the file.\n");
  9.         }
  10.         fwrite(arr, 25*sizeof(int), 1, fp);
  11.         fclose(fp);
  12.         if((fp=fopen("a.txt","rb"))==NULL){
  13.                 printf("Fail to open the file.\n");
  14.         }
  15.        //修改文件打开方式
  16.         fread(brr, 25*sizeof(int), 1, fp);
  17.         fclose(fp);      
  18.         printf("%d", brr[1][1]);
  19.                 return;
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-21 18:43:13 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +1 鱼币

  1. void main(void)
  2. {
  3.         int arr[5][5]={0,0,0,0,0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4};
  4.         int brr[5][5];
  5.         FILE *fp;
  6.        //修改文件打开方式
  7.         if((fp=fopen("a.txt","wb"))==NULL){
  8.                 printf("Fail to open the file.\n");
  9.         }
  10.         fwrite(arr, 25*sizeof(int), 1, fp);
  11.         fclose(fp);
  12.         if((fp=fopen("a.txt","rb"))==NULL){
  13.                 printf("Fail to open the file.\n");
  14.         }
  15.        //修改文件打开方式
  16.         fread(brr, 25*sizeof(int), 1, fp);
  17.         fclose(fp);      
  18.         printf("%d", brr[1][1]);
  19.                 return;
  20. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-21 23:12:31 | 显示全部楼层

啊对我没注意到后面要换一种方式打开!谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 12:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表