鱼C论坛

 找回密码
 立即注册
查看: 1155|回复: 6

[已解决]C,文件输入

[复制链接]
发表于 2017-4-24 21:54:26 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 GSMNC 于 2017-4-24 21:56 编辑

各位大神求指教,为什么我的代码编译通过,但测试出错
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define total 5

  4. int main()
  5. {
  6.     int i;
  7.     char ch;
  8.     char filename[10];
  9.     FILE *fp;

  10.     struct student
  11.     {
  12.         char name[10];
  13.         char sex;
  14.         int num;
  15.         int score;
  16.     }a[total];

  17.     printf("Please input student's information,including name,sex,num,score.\n");
  18.     for(i = 0; i < total; i++)
  19.     {

  20.         scanf("%s %c %d %d", a[i].name, &a[i].sex, &a[i].num, &a[i].score);
  21.     }

  22.     printf("Enter file's name.\n");
  23.     scanf("%s", filename);

  24.     if ((fp = fopen(filename, "r+")) == NULL)
  25.     {
  26.         printf("Open operation failed.\n");
  27.         exit(0);
  28.     }
  29.     ch = getchar();

  30.     for (i = 0; i < total; i++)
  31.     {
  32.         if ((fwrite(&a[i], sizeof(struct student), 1, fp)) != 1)
  33.         {
  34.             printf("write file fails\n");
  35.         }

  36.     }
  37.     fclose(fp);

  38.     printf("Hello world!\n");
  39.     return 0;
  40. }
复制代码
最佳答案
2017-4-25 14:45:45
fwrite这个函数以二进制形式对文件进行操作。它写入文件是二进制写入的,所以是乱码。
可以使用fprintf函数写入文件。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define total 5

  4. int main()
  5. {
  6.     int i;
  7.     char c='\n';
  8.     char ch;
  9.     char filename[10];
  10.     FILE *fp;

  11.     struct student
  12.     {
  13.         char name[10];
  14.         char sex;
  15.         int num;
  16.         int score;
  17.     }a[total];

  18.     printf("Please input student's information,including name,sex,num,score.\n");
  19.     for(i = 0; i < total; i++)
  20.     {

  21.         scanf("%s %c %d %d", a[i].name, &a[i].sex, &a[i].num, &a[i].score);
  22.     }

  23.     printf("Enter file's name.\n");
  24.     scanf("%s", filename);

  25.     if ((fp = fopen(filename, "r+")) == NULL)
  26.     {
  27.         printf("Open operation failed.\n");
  28.         exit(0);
  29.     }
  30.     ch = getchar();

  31.     for (i = 0; i < total; i++)
  32.    {
  33.       /* if ((fwrite(a, sizeof(struct student), 3, fp)) != 1)
  34.         {
  35.             printf("write file fails\n");
  36.         }*/
  37.         if(fprintf(fp,"%s %c %d %d %c", a[i].name,a[i].sex,a[i].num,a[i].score,c)<0)
  38.         {

  39.             printf("write file fails\n");
  40.         }

  41.     }
  42.     fclose(fp);

  43.     //printf("Hello world!\n");
  44.     return 0;
  45. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-24 22:21:42 | 显示全部楼层
有两处错误:
1、第31行,文件打开方式应该为a+(写追加)
2、第40行,写文件方式不合适,写出的内容是乱码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-24 22:21:51 | 显示全部楼层
报错是什么?
我只看到你这个if ((fp = fopen(filename, "r+")) == NULL)
这里只有只读权限没有写权限,然后你后边有写操作肯定不行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 14:45:45 | 显示全部楼层    本楼为最佳答案   
fwrite这个函数以二进制形式对文件进行操作。它写入文件是二进制写入的,所以是乱码。
可以使用fprintf函数写入文件。
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define total 5

  4. int main()
  5. {
  6.     int i;
  7.     char c='\n';
  8.     char ch;
  9.     char filename[10];
  10.     FILE *fp;

  11.     struct student
  12.     {
  13.         char name[10];
  14.         char sex;
  15.         int num;
  16.         int score;
  17.     }a[total];

  18.     printf("Please input student's information,including name,sex,num,score.\n");
  19.     for(i = 0; i < total; i++)
  20.     {

  21.         scanf("%s %c %d %d", a[i].name, &a[i].sex, &a[i].num, &a[i].score);
  22.     }

  23.     printf("Enter file's name.\n");
  24.     scanf("%s", filename);

  25.     if ((fp = fopen(filename, "r+")) == NULL)
  26.     {
  27.         printf("Open operation failed.\n");
  28.         exit(0);
  29.     }
  30.     ch = getchar();

  31.     for (i = 0; i < total; i++)
  32.    {
  33.       /* if ((fwrite(a, sizeof(struct student), 3, fp)) != 1)
  34.         {
  35.             printf("write file fails\n");
  36.         }*/
  37.         if(fprintf(fp,"%s %c %d %d %c", a[i].name,a[i].sex,a[i].num,a[i].score,c)<0)
  38.         {

  39.             printf("write file fails\n");
  40.         }

  41.     }
  42.     fclose(fp);

  43.     //printf("Hello world!\n");
  44.     return 0;
  45. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 20:28:05 | 显示全部楼层
菟纸先森 发表于 2017-4-24 22:21
有两处错误:
1、第31行,文件打开方式应该为a+(写追加)
2、第40行,写文件方式不合适,写出的内容是乱 ...

1、改为“a+”,依然不对。。。
2、40这样写是乱码,能问下原因吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 20:29:33 | 显示全部楼层
lumber2388779 发表于 2017-4-24 22:21
报错是什么?
我只看到你这个if ((fp = fopen(filename, "r+")) == NULL)
这里只有只读权限没有写权限, ...

谢谢您,我翻看了下书,“r+”文件使用方式既可读也可写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 20:33:28 | 显示全部楼层
bajo 发表于 2017-4-25 14:45
fwrite这个函数以二进制形式对文件进行操作。它写入文件是二进制写入的,所以是乱码。
可以使用fprintf函 ...

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 05:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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