user-0 提问: fprintf输出相关问题

回复:
user: user-1
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  struct course {     int num;                               /*课程编号*/     char name[30];                         /*课程名称*/     char kind[20];                         /*课程性质*/     int time;                              /*课时*/     int mark;                              /*学分*/     int term;                              /*1为考试2为考察*/     int opterm;                            /*开课学期*/ };  typedef struct Node                  /*创建头结点*/ {     struct course d;     struct Node *next; }Node;     /*课程插入*/ void insert() {     FILE *fp1;     int g=1,key=0,key1=0,f=0;     int a=0,b=0;     if((fp1=fopen("course.txt","r"))==0)     {         printf("can't open file\n");         exit(0);     }      Node *first=(Node *)malloc(sizeof(Node));           /*头指针初始化*/     //first->next=0;     first->next= NULL;     for(;!feof(fp1);)     {         Node *s=(Node*)malloc(sizeof(Node));         //fscanf(fp1,"%d%s%s%d%d%d%d", &s->d.num,&s->d.name,&s->d.kind,&s->d.time,&s->d.mark,&s->d.term,&s->d.opterm);         fscanf(fp1,"%d%s%s%d%d%d%d", &s->d.num, s->d.name, s->d.kind,&s->d.time,&s->d.mark,&s->d.term,&s->d.opterm);         s->next=first->next;         first->next=s;     }      for(;g==1;)     {         printf("请输入需要插入的课程信息:\n课程编号，课程名称，课程性质，课时，学分，1为考试2为考察，开课学期\n");         Node *e=(Node*)malloc(sizeof(Node));         //scanf("%d%s%s%d%d%d%d",&e->d.num,&e->d.name,&e->d.kind,&e->d.time,&e->d.mark,&e->d.term,&e->d.opterm);         scanf("%d%s%s%d%d%d%d",&e->d.num, e->d.name, e->d.kind,&e->d.time,&e->d.mark,&e->d.term,&e->d.opterm);         e->next=first->next;         first->next=e;         printf("是否继续输入？1为是，2为否\n");         key=0;         while(key==0){             scanf("%d",&g);             if(g==1||g==2)                 key=1;             else             {                 printf("\n输入错误，请重新输入！！！\n");                 printf("是否继续输入？1为是，2为否\n");             }         }     }     fclose(fp1);     printf("是否存盘？？1为是2为否\n");     while(key1==0){         scanf("%d",&f);         if(f==1||f==2)             key1=1;         else             printf("\n输入错误，请重新输入！！！\n");     }     if(f==1)     {         //if((fp1=fopen("course.txt","w"))==0)         if((fp1=fopen("course.txt","w"))==NULL)         {             printf("can't open file\n");             exit(0);         }         Node *p=first->next;         while(p!=0){             fprintf(fp1,"%5d%12s%9s%9d%9d%11d%11d", p->d.num,p->d.name,p->d.kind,p->d.time,p->d.mark,p->d.term,p->d.opterm);             p = p->next;         }     }     printf("录入完毕\n");     Node *k=first;     //while(first!=0){     while(first!= NULL){         first=first->next;         free(k);         k=first;     }     fclose(fp1);     while(b==0){         printf("\n1-返回主菜单；2-退出系统\n");         scanf("%d",&a);         if(a==1||a==2)             b=1;         else             printf("\n输入错误，请重新输入！！！\n");     }     if(a==1)         //main();         return ;     else         exit(0); }    /*主菜单*/ void main() {     int n,w = 0;     printf("*********************************************************************************\n");     printf("                                      学生选课系统菜单\n");     printf("*********************************************************************************\n");     printf("                                      1-录入\n");     printf("                                      2-浏览\n");     printf("                                      3-查询\n");     printf("                                      4-修改\n");     printf("                                      5-删除\n");     printf("                                      6-统计\n");     printf("                                      0-退出\n");     printf("*********************************************************************************\n");     printf("                请输入选项前的数字\n");     scanf("%d",&n);     do     {         if(n>5||n<0)         {             printf("\n输入错误\n");             scanf("%d",&n);             w = 0;         }         else             w=1;     }while(w==0);     switch(n)     {         case 1:insert();break;         case 2:browser();break;         case 3:search();break;         case 4:revise();break;         case 5:statistics();break;         case 6:delet();break;         case 0:exit(0);     } }  供参考～ 建议对比代码找出自己的问题，另外把代码功能补全了～

user: user-0
content: 第一个是多打了一行，第二个是数组输出的时候会传首地址，然后0要改成null，函数要有返回值，是这些吗？ 前面理解了，但是：0和null应该只是表现形式不一样吧？函数要有返回值，但是我的函数类型是void，用return空有什么好处吗？