免费注册 查看新帖 |

Chinaunix

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

[C] [讨论]文件读写操作问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-18 21:20 |只看该作者 |倒序浏览
5可用积分
期末了我想写一个简单的程序,目标是将学生考试的客观题答案事先在Windows下输入文本文档保存起来,每输入完毕一个学生的所有答题结果(用大写字母表示)就换行输入下一个学生的答题结果直到全部答题输入完毕。然后用程序打开刚才的文本文档逐行读入学生的成绩,然后将每一行学生的成绩与答案字符串(用答鞋子目表示)进行比较之后计算该学生成绩与错误题号。以下是我的源码,但有一个问题,比方来说,我明明在保存学生答题结果的文本文档中输入的是四个学生的成绩,但在输出学生成绩情况的文本文档中确出现了八个输出结果。不知这是何原因所致,请教各位,谢谢!



/* File-based paper grading */

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

#define SIZE 81
#define LEN 10

void grader(char * line,FILE * spew);

int main(void){
   
    char filnam[SIZE],filnam_1[SIZE],temp[LEN];
    FILE * feed,* spew;
   
    printf("\nEnter the name of the input file: ");
    gets(filnam);
    printf("\nThen enter the name of the output file: ");
    gets(filnam_1);
    if((feed = fopen(filnam,"r")) == NULL){
        fprintf(stderr,"\nUnable to open file %s.\n",filnam);
        system("PAUSE");
        exit(EXIT_FAILURE);
    }
    if((spew = fopen(filnam_1,"a")) == NULL){
        fprintf(stderr,"\nUnable to create file %s.\n",filnam_1);
        system("PAUSE");
        exit(EXIT_FAILURE);
    }
    while(fgets(temp,LEN, feed) != NULL)
        grader(temp,spew);
    if(fclose(feed) != 0)
        fprintf(stderr,"\nUable to close file %s.\n",filnam);
    if(fclose(spew) != 0)
        fprintf(stderr,"\nUnable to close file %s.\n",filnam_1);
    puts("\nDone!");
    system("PAUSE");
    return 0;
}

void grader(char * line, FILE * spew){
     
     char key[LEN] = "CBDBADBBBC";
     int i;
     int mark = 10;
     int wrngnum[LEN] = {0};
     
     for(i = 0; i < LEN; i++)
           if(line != key){
               wrngnum = i + 1;
               mark--;
           }
     fprintf(spew,"\nThe mark for the test item is %d.\n"
                  "The sequence numbers of the wrong answer are: \n",mark);
     for(i = 0; i < sizeof(wrngnum)/sizeof(wrngnum[0]); i++)
         if(wrngnum != 0)
             fprintf(spew,"%3d",wrngnum);
}



保存学生答题结果的文本文档中的内容是:

CDADBDBCCD
CDABBDBCCA
CBDBADBDBC
BCDBAABCBC


输出学生成绩情况的文本文档中却显示出以下结果:


The mark for the test item is 3.
The sequence numbers of the wrong answer are:
  2  3  4  5  8  9 10
The mark for the test item is 2.
The sequence numbers of the wrong answer are:
  1  2  3  4  5  8  9 10
The mark for the test item is 4.
The sequence numbers of the wrong answer are:
  2  3  5  8  9 10
The mark for the test item is 3.
The sequence numbers of the wrong answer are:
  1  2  3  5  8  9 10
The mark for the test item is 8.
The sequence numbers of the wrong answer are:
  8 10
The mark for the test item is 6.
The sequence numbers of the wrong answer are:
  2  3  8 10
The mark for the test item is 5.
The sequence numbers of the wrong answer are:
  1  2  6  8 10
The mark for the test item is 5.
The sequence numbers of the wrong answer are:
  2  3  6  8 10

最佳答案

查看完整内容

while(fgets(temp, LEN + 2, feed) != NULL) { temp[strlen(temp) -1] = 0; grader(temp,spew); }你把你temp中的内容打印出来就知道原因了。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2008-01-18 21:20 |只看该作者
while(fgets(temp, LEN + 2, feed) != NULL)
    {
        temp[strlen(temp) -1] = 0;
        grader(temp,spew);
    }
你把你temp中的内容打印出来就知道原因了。

论坛徽章:
0
3 [报告]
发表于 2008-01-18 21:53 |只看该作者
楼主能编译的过?wrngnum是整形数组,后边确当成整数使用

论坛徽章:
0
4 [报告]
发表于 2008-01-18 23:14 |只看该作者
原帖由 hejt730 于 2008-1-18 21:53 发表
楼主能编译的过?wrngnum是整形数组,后边确当成整数使用

我的源代码里本来是“wrngnum”的,不知怎么搞的,复制到这里就变成这样了。

论坛徽章:
0
5 [报告]
发表于 2008-01-18 23:17 |只看该作者
原帖由 lenovo 于 2008-1-18 22:32 发表
while(fgets(temp, LEN + 2, feed) != NULL)
    {
        temp[strlen(temp) -1] = 0;
        grader(temp,spew);
    }
你把你temp中的内容打印出来就知道原因了。

这个我也试过了,还是可以的,不过这样做有点累赘了。

[ 本帖最后由 mcmay 于 2008-1-18 23:22 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-01-18 23:21 |只看该作者
原帖由 lenovo 于 2008-1-18 22:32 发表
while(fgets(temp, LEN + 2, feed) != NULL)
    {
        temp[strlen(temp) -1] = 0;
        grader(temp,spew);
    }
你把你temp中的内容打印出来就知道原因了。

我把 “while(fgets(temp,LEN,feed) != NULL)
               grader(temp,spew); ”
换成:
      “while(fgets(temp,LEN+2,feed) != NULL)
               grader(temp,spew); ”

就好了。不过,不知这是为什么呢?

[ 本帖最后由 mcmay 于 2008-1-18 23:31 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2008-01-18 23:35 |只看该作者
原帖由 mcmay 于 2008-1-18 23:21 发表

我把 “while(fgets(temp,LEN,feed) != NULL)
               grader(temp,spew); ”
换成:
      “while(fgets(temp,LEN+2,feed) != NULL)
               grader(temp,spew); ”

就好了。不过,不 ...

已经告诉你方法了。
自己试着解决吧。

论坛徽章:
0
8 [报告]
发表于 2008-01-19 22:19 |只看该作者
fget函数会自动在读出的窜后加回车,在加上最后得'\0',应该至少12个字节,楼主定义的长度太小了,越界了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP