|
|
发表于 2012-7-19 12:20:30
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- #define SUN 4
- void sunyi();
- struct student_type
- {
- char name[10];
- int num;
- int age;
- char addr[30];
- };
- struct student_type stud[SUN]; //定义为 全局数组 以方便 在sunyi 函数里面需要用到
- void main()
- {
- 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 ) //你本来想引用 stud 数组的 但是 你在主函数定义的 作用于只限于主函数
- {
- printf("写入错误\n");
- fclose(fp);
- }
- }
- printf("已完成写入-_-");
- // return (); 这里的 你返回什么 ? 记住 void 的2个作用 1. 对函数参数的限定 2. 对函数返回的限定
- }
复制代码 |
|