鱼C论坛

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

编译错误0.0 求助

[复制链接]
发表于 2012-7-19 10:35:04 | 显示全部楼层 |阅读模式

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

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

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

#define SUN 4
void sunyi();
struct student_type
{
        char name[10];
        int num;
        int age;
        char addr[30];
};

void main()
{
        struct student_type stud[SUN];
        int i;
        printf("请分别输入->名字->学号->年龄->地址:\n");
        for(i=0;i<SUN;i++)
        {
                scanf("%s,%d,%d,%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
        }
       
        sunyi();


}
void sunyi()
{
        FILE *fp;
        int i;
        if(!(fp=fopen("shujujiegou","wb")))
        {
                printf("无法打开文件\n");
                system("pause");
        }
        for(i=0;i<SUN;i++)
        {
                if( fwrite(&stud[i], sizeof(struct student_type), 1, fp) != 1 )  //就这句编译出错 不知道怎么搞的??
                {
                                                printf("写入错误\n");
                        fclose(fp);
                }
        }
        printf("已完成写入-_-");
        return();

}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-19 11:15:57 | 显示全部楼层

回帖奖励 +3 鱼币

fwrite(&stud[i], ....// 这里的 stud[i]是在哪里定义的~~~ 你在这个函数里面能用吗
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-19 12:20:30 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define SUN 4

  4. void sunyi();

  5. struct student_type
  6. {
  7.         char name[10];
  8.         int num;
  9.         int age;
  10.         char addr[30];
  11. };

  12. struct student_type stud[SUN];                //定义为 全局数组 以方便 在sunyi 函数里面需要用到

  13. void main()
  14. {
  15.         int i;
  16.         printf("请分别输入->名字->学号->年龄->地址:\n");
  17.         for(i=0;i<SUN;i++)
  18.         {
  19.                 scanf("%s,%d,%d,%s",&stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
  20.         }
  21.        
  22.         sunyi();
  23.        
  24.        
  25. }
  26. void sunyi()
  27. {
  28.         FILE *fp;
  29.         int i;
  30.         if(!(fp=fopen("shujujiegou","wb")))
  31.         {
  32.                 printf("无法打开文件\n");
  33.                 system("pause");
  34.         }
  35.         for(i=0;i<SUN;i++)
  36.         {
  37.                 if( fwrite(&stud[i], sizeof(struct student_type), 1, fp) != 1 )  //你本来想引用 stud 数组的 但是 你在主函数定义的 作用于只限于主函数
  38.                 {
  39.                         printf("写入错误\n");
  40.                         fclose(fp);
  41.                 }
  42.         }
  43.         printf("已完成写入-_-");
  44.         // return ();  这里的 你返回什么 ?  记住 void 的2个作用 1. 对函数参数的限定 2. 对函数返回的限定
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-21 14:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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