设计一个储存学生信息的程序,但是在读取过程中想根据输入数据调取相关学生信息,一直显示读取访问冲突。
主要问题大概在这一部分吧
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct ch
{
char name;
int scores;
}tj[3];
int shuru(char *strFile)
{
int i, scores[3];
char name[20];
FILE *fp;
fp = fopen("d:\\123.txt", "w");
if (fp == NULL)
{
perror("fopen");
return -1;
}
printf("请输入:\n");
for (i = 0; i < 3; i++)
{
printf("input name:\n");
scanf("%s", name);
fprintf(fp, "%s\n", name);
printf("input scores:\n");
scanf("%s", scores);
fprintf(fp, "%s\n", scores);
}
printf("信息成功保存!");
fclose(fp);
}
struct grope
{
char mingzi;
int fenshu;
}gro;
void scan()
{
char ways;
int flag1 = 0, flag2 = 0;
printf(" 请输入查询方式:\n 1.姓名查询\n 2.成绩查询\n ");
FILE *fp = fopen("D:\\123.txt", "rb");
ways = _getch();
if (ways == '1')
{
printf("请输入姓名:");
scanf("%s", &gro.mingzi);
for (int i = 0; !feof(fp); i++)
{
fread(&tj[i], sizeof(struct ch), 1, fp);
if (tj[i].name == gro.mingzi)
{
printf("信息如下:\n");
printf("姓名:%s ", tj[i].name, tj[i].scores);//这一行开始 ,数据就没法输出了
flag1 = 1;
}
}
fclose(fp);
if (flag1 == 0)
{
printf("请重新输入!");
}
}
if (ways == '2')
{
printf("请输入成绩:\n");
scanf_s("%s", gro.fenshu);
for (int i = 0; !feof(fp); i++)
{
fread(&tj[i], sizeof(struct ch), 1, fp);
if (tj[i].scores == gro.fenshu)
{
printf("信息如下:\n");
printf("姓名:%s 成绩:%s", tj[i].name, tj[i].scores);
}
}
}
fclose(fp);
if (flag2 == 0)
{
printf("请重新输入!");
}
}
int main()
{
int i;
scanf_s("%d", &i);
char file[20] = "data.txt";
switch (i)
{
case 1:
shuru(file);
break;
case 2:
scan();
break;
}
return 0;
}
可能是一个变量或者一个数组或者是一个结构,有两个值,输出不行,冲突