{
char name[20]; /*姓名*/
int age; /*年龄*/
long stu_ID; /*学号*/
};
void main()
{
struct student *p1,*p2;
scanf("%s%d%ld",&p1.name,&p1.age,&p1.stu_ID);
}
这个意思吧 我觉得差不多
----------------解决方案--------------------------------------------------------
运行下看看么就知道了歪。
多测试测试。结果很容易就出来了!
我不想搞,没有东西可以看。。。光想着晕呼。。
----------------解决方案--------------------------------------------------------
测试了一下。C-FREE3.5
在结构体中不要用&p.name只类的
而是该用“-> ”这个符号。
再整理下输入就好了。
----------------解决方案--------------------------------------------------------
#include <stdio.h>
struct student
{
char name[20]; /*姓名*/
int age; /*年龄*/
long stu_ID; /*学号*/
};
void main()
{
struct student p1,p2;
scanf("%s%d%ld",&p1.name,&p1.age,&p1.stu_ID);
printf(" %s \t %d \t %ld \n",p1.name,p1.age,p1.stu_ID);
}
这样是可以的 但用了* 就有问题 呵呵 搞不来了
----------------解决方案--------------------------------------------------------
-.-完全无视我说的。。。
----------------解决方案--------------------------------------------------------
定义一个结构体类型,包括3个成员
姓名 20;年龄 int; 学号 12。注意此人不断修改题目
定义两个结构体变量,定义两个结构体指针变量,编写从键盘输入2个人的信息,把第一个人的信息传送到第一个结构体变量,把第二个人的信息传送到第二个结构体变量。
把第一个和第二个传送到结构体指针变量中去,用fread函数将第一个人的信息和第二个信息保存到文件中去,保存名要从键盘输入。注意题目有误,应为fwrite函数
#include "stdio.h"
#include "stdlib.h"
struct student
{
char name[20];
short int age;
char stu_ID[12];
};
void input(struct student *);
void main()
{
struct student a,b,*pa=&a,*pb=&b;
FILE *fp=fopen("c:\\student2.dat","wb");
if(fp==NULL)
{
fprintf(stderr,"data file(s) cannot be created...\n");
abort();
}
input(pa);input(pb);
fwrite(pa,sizeof(a),1,fp);
fwrite(pb,sizeof(b),1,fp);
fclose(fp);
}
void input(struct student *p)
{
static int i;
printf("\n请输入第%d人的情况\n",++i);
printf("姓名=");scanf("%s",p->name);
printf("年龄=");scanf("%d",&p->age);
printf("学号=");scanf("%s",p->stu_ID);
}
----------------解决方案--------------------------------------------------------
刚刚 我再敲呀 没看到 那句
不过 .. 不太明白 看了也白看
----------------解决方案--------------------------------------------------------
好东西 COPY 下来 HOHO
]
不过 我感觉好想有点不对
----------------解决方案--------------------------------------------------------
void input(struct student *);
void main()
{
struct student a,b,*pa=&a,*pb=&b;
FILE *fp=fopen("c:\\student2.dat","wb");
if(fp==NULL)
{
fprintf(stderr,"data file(s) cannot be created...\n");
abort();
}
input(pa);input(pb);
fwrite(pa,sizeof(a),1,fp);
fwrite(pb,sizeof(b),1,fp);
fclose(fp);
}
★ 解释下这段 有点不能理解
----------------解决方案--------------------------------------------------------
/*大家注意:
C盘根目录下的student2.dat是二进制
文件,要用下面程序才能把数据调出来
*/
#include "stdio.h"
#include "stdlib.h"
struct student
{
char name[20];
short int age;
char stu_ID[12];
};
void main( )
{ int i;
struct student stu[2],*p=stu;
FILE *fi=fopen("c:\\student2.dat","rb");
if(fi==NULL)
{
fprintf(stderr,"file(s) not found...\n");
abort();
}
fread(p,sizeof(struct student),2,fi);
fclose(fi);
for(i=0;i<2;i++,p++)
{
printf("第%d人的情况:\n",i+1);
printf("姓名%s\n",p->name);
printf("学号%s\n",p->stu_ID);
printf("年龄%d\n\n",p->age);
}
}
----------------解决方案--------------------------------------------------------