当前位置:编程学习 > C/C++ >>

C语言: 无法实现想要的功能

#include<stdio.h>
#include<string.h>
#define A "please input"
struct student
{
char name[15];
int math;
int chinese;
int english;
};
int main()
{
struct student stu[10];
int i,n;
char a[100],filename[100];
FILE *fp;
printf(A" filename:\n");
scanf("%s",filename);
printf("please input number of students:\n");
scanf("%d",&n);
printf("please input information:\n");
for(i=0;i<n;i++)
{
printf(A" name:\n");
scanf("%s",stu[i].name);
printf(A" math:\n");
scanf("%d",&stu[i].math);
printf(A" chinese:\n");
scanf("%d",&stu[i].math);
printf(A" english:\n");
scanf("%d",&stu[i].english);
}
if((fp=fopen(filename,"wb"))==NULL)
printf("error!!");
for(i=0;i<n;i++)
fwrite(&stu[i],sizeof(struct student),1,fp);
fclose(fp);
return 0;

}


能在知道路径建立文件但是文件内容是乱码

追问:那应该怎么改呢。用fprintf的话name这一块无法输入
答案:你的问题和我的问题是一样的我以前也遇到过,就是fwrite的问题,不能将int类型的写入文件

struct student stu[10];
int i,n;
char a[100],filename[100];
FILE *fp;
printf(A" filename:\n");
scanf("%s",filename);
printf("please input number of students:\n");
scanf("%d",&n);
printf("please input information:\n");
for(i=0;i<n;i++)
{
printf(A" name:\n");
scanf("%s",stu[i].name);
printf(A" math:\n");
scanf("%d",&stu[i].math);
printf(A" chinese:\n");
scanf("%d",&stu[i].chinese);
printf(A" english:\n");
scanf("%d",&stu[i].english);
}
if((fp=fopen(filename,"w"))==NULL)
printf("error!!");
for(i=0;i<n;i++)
fprintf(fp,"name:%s\tmath:%d\tchinese:%d\tenglish:%d\n",stu[i].name,stu[i].math,stu[i].chinese,stu[i].english);
fclose(fp);

上一个:拜求一个简单的C语言密码程序!!!
下一个:6进制转化成10进制的c语言程序

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,