| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 541 人关注过本帖
标题:求助 文件写入问题
取消只看楼主 加入收藏
klpk521
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-28
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:2 
求助 文件写入问题
设计一个学生作业管理程序 即使什么都没有操作直接退出程序也会向文件写入一堆看不懂的数据
另外 delete_one 函数也运行不了
还有用链表求平均值如何求?
求高
程序代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
#define INPUT %d %d %s %d %d %d %d %d %d %d %d %d %d
#define SCANF &pC->num,&pC->Class,pC->name,&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]
#define PRINT pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]
struct student
{
    int num;
    int Class;
    char name[10];
    int finish[10];
    struct student * next;
};
   
struct student *insert(struct student *head,
                       struct student *pNode)
{
    // pPrev为前驱结点位置,pNext为后继结点位置
    struct student *pPrev,*pNext;
    pNext = head;
    if(head == NULL) {     //空链表
        head = pNode;      //插入到第一个位置
        pNode->next = NULL;
    }else{                 //非空链表                      
        /*
        查找插入位置。
        将pNode->num与pNext->num相比较,
        如果pNode->num > pNext-> num ,
        则待插入的结点应插在pNext所指的结点之后的某个位置,
        此时将pNext后移,并使pPrev指向刚才pNext所指的结点.
        */
        while((pNext != NULL) &&
              (pNode->num > pNext->num))  {
            pPrev = pNext;   
            pNext = pNext->next;
        }
            
        if (pNext == NULL)  {   //未找到插入位置
            pPrev->next = pNode;//插入到链表最后位置
            pNode->next = NULL;
        }else{
            if(head == pNext)   //第一个位置
                head = pNode;
            else                //其他位置
                pPrev->next = pNode;    
          
            pNode->next = pNext;
        }
    }
    return head ;
}
struct student * add_score()
{
    struct student *head = NULL;
    struct student *pC,*pEnd;
    pC =pEnd =(struct student*) malloc(LEN);
    puts("请输入学号(输入0退出):");
    scanf("%d",&pC->num);
    if(pC->num==0)
    {return head;}
    else
    puts("\n请输入班级(如:1):");
    scanf("%d",&pC->Class);
    puts("\n请输入姓名:");
    scanf("%s",pC->name);
    puts("\n请输入十次成绩:");
    scanf("%d %d %d %d %d %d %d %d %d %d",&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
    while(pC->num>0)
    {
        if(head==NULL)
        {
            head=pC;
        }
        else
        {
            pEnd->next = pC;
        }
        pEnd=pC;
   
        head = insert(head , pC);
   
        pC=(struct student*)malloc(LEN);
      
        puts("请输入学号(输入0退出):");
        scanf("%d",&pC->num);
        if(pC->num==0)
        {return head;}
        else
        puts("\n请输入班级(如:一班):");
          scanf("%d",&pC->Class);
        puts("\n请输入姓名:");
         scanf("%s",pC->name);
        puts("\n请输入十次成绩:");
         scanf("%d %d %d %d %d %d %d %d %d %d",&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
    }
    pEnd->next=NULL;
    return head;
}
struct student *display_one(struct student *head, int num)
{
    struct student *pC = head;
    if(num==0)
    {return head;}
    else
    {
        if(pC==NULL)
        puts("数据不存在.");
        else
        {
            while(pC != NULL && num != pC->num)
            {
                pC = pC->next;
            }
            if(pC==NULL)
            {
                printf("找不到该学号!\n");
            }
          
            else
            printf("%d %d %s %d %d %d %d %d %d %d %d %d %d \n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
        }
    }
    return head;
}



struct student *display_class(struct student *head,int Class)
{
    struct student *pC = head;
    while(pC!=NULL && Class==pC->Class)
    {
        printf("%d %d %s %d %d %d %d %d %d %d %d %d %d\n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
        pC=pC->next;
    }
    return head;
}
struct student *alter(struct student *head,int num)
{
    int i,score;
    struct student *pC=head;
  
    if(head==NULL)
    {
        printf("学号不存在.");
        return NULL;
    }
    while(pC!=NULL && num!=pC->num)
    {
        pC=pC->next;
    }
   printf("pC == %p\n", pC);
    while(i!=0)
    {
        printf("您想要更改第几次作业情况?(输入0退出)\n");
           scanf("%d",&i);
           if(i!=0)
           {
               printf("\n请输入分数:");
               scanf("%d",&pC->finish[i-1]);
           }
    }
    return head;
}
struct student *delete_one(struct student *head, int num)
{
    struct student *pC,*pP;
    pC = head;
    if (pC == NULL)
    { 
        printf("没有数据.\n");
        return NULL;
    }
      printf("still");
    while (pC != NULL && num != pC->num)
    {
         pP = pC;
         pC = pC->next;
    }
  
    if(pC!=NULL)
    {
        printf("删除学号:%d\n", num);
      
        if  (pC == head)
        {
            head = pC->next;                
        }
        else
        {               
            pP->next = pC->next;
        }
        free(pC);
    }
    else
    {
        printf("学号 %d 不存在!\n",num);
    }
  
    return head;      
}
//struct student *
void fileread(struct student *head)
{
    FILE *fp;
    struct student *pC = head;
    if((fp=fopen("record.txt","r"))==NULL)
    {
        printf("文件打开错误!");
    }

    while(pC!=NULL)
    {
   
        fscanf(fp,"%d %d %s %d %d %d %d %d %d %d %d %d %d\n",&pC->num,&pC->Class,pC->name,&pC->finish[0],&pC->finish[1],&pC->finish[2],&pC->finish[3],&pC->finish[4],&pC->finish[5],&pC->finish[6],&pC->finish[7],&pC->finish[8],&pC->finish[9]);
        pC=pC->next;
    }
    fclose(fp);
}

void filesave(struct student *head)
{
    FILE *fp;
    struct student *pC=head;

    if((fp=fopen("record.txt","w+"))==NULL)
    {
        printf("文件打开错误!");
        exit(0);
    }

    if(pC==NULL)
        printf("error");
    else
    {
        while(pC!=NULL)
        {
            fprintf(fp,"%d %d %s %d %d %d %d %d %d %d %d %d %d\n",pC->num,pC->Class,pC->name,pC->finish[0],pC->finish[1],pC->finish[2],pC->finish[3],pC->finish[4],pC->finish[5],pC->finish[6],pC->finish[7],pC->finish[8],pC->finish[9]);
            pC=pC->next;
        }
    }
    fclose(fp);
}

int main()
{
    struct student * head ;
    int num;
    int choice,Class;

    fileread(head);

    while (choice!=7)
    {
        printf("\t\t\t欢迎进入学生作业完成情况管理系统\n");
        printf("\t\t\t--------------------------------\n\n\n");
        printf("1.录入某位学生某次作业的分数\n");
        printf("2.给定学号,显示某位学生的作业完成情况\n");
        printf("3.给定某个班级的班号,显示该班所有学生的作业完成情况\n");
        printf("4.给定某位学生的学号,修改该学生的作业完成信息\n");
        printf("5.给定某位学生的学号,删除该学生的信息\n");
        printf("6.提供一些统计各类信息的功能\n");
        printf("7.退出\n\n");
        printf("请选择功能:");

    scanf("%d",&choice);

    switch (choice)
    {
    case 1:
    add_score();break;

    case 2:
    printf("请输入学号:");
    scanf("%d",&num);
    display_one(head,num);break;

    case 3:
    printf("请输入班级:");
    scanf("%d",&Class);
    display_class(head,Class);break;

    case 4:
    printf("请输入学号:");
    scanf("%d",&num);
    alter(head,num);break;

    case 5:
    printf("请输入学号:");
    scanf("%d",&num);
    delete_one(head,num);break;

    case 6:
    break;

    case 7:
    break;
    }
    }
    filesave(head);

    return 0;
}
人点拨
搜索更多相关主题的帖子: 如何 color 平均值 管理程序 设计 
2011-12-15 20:29
klpk521
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-28
收藏
得分:0 
麻烦帮看看
2011-12-16 10:25
klpk521
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-28
收藏
得分:0 
回复 3楼 silent_world
1.fileread 函数里面对每个数据结构的读取是否要在循环里加malloc?(助教说的)
2.怎么操作才能建立起链表而不是只是传入了*head?

经过很多次测试发现fileread函数的问题很多 是否能写一个完整的函数?  十分感谢
2011-12-16 12:00
快速回复:求助 文件写入问题
数据加载中...
 
   



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

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