读书人

自己编写的一个简单的搜索程序程序没

发布时间: 2013-03-01 18:33:02 作者: rapoo

求助 自己编写的一个简单的搜索程序,程序没有报错,但是运行时会中止,麻烦高手看下
代码如下
#include <stdio.h>
#include <string.h>
#include "stdlib.h"

struct Student_type{
char name[10];
char *explanation[10];
};


static int main() {
FILE *fp;
struct Student_type stud[10];
if ((fp = fopen("test1.txt", "r")) == NULL) {
printf("No Student Infomation Existed\n");
exit(0);
}
int i = 0;
while (! feof(fp)) {
fscanf(fp, "%s %s\n", &stud[i].name, stud[i].explanation);
i ++;
};
printf("%s %s\n",stud[1].name, stud[1].explanation );


printf("*************************\n");
printf("请输入查询参数:\n");

int mark = 0;
int j;
char name;
scanf("%s", &name);
printf("%s\n", &name);
for(j=0;j<=10;j++)
{if(strcmp(stud[j].name,&name) == 0)
printf("%s %s\n", *stud[j].name, stud[j].explanation),
mark=1;
}
if(mark == 0)
printf("No match!\n");

fclose(fp);
return 0;

}

运行了一下程序没报错,但是判断时程序中断,自己实在弄不清楚,麻烦高手来看一下啊。 字符串 求助
[解决办法]
不是,刚才没注意,好多问题,首先对于*和&的使用感觉你不是很理解吧,还有scanf和printf的使用也不太对好像
我修改了一下,不知道是不是这样


#include <stdio.h>
#include <string.h>
#include "stdlib.h"

struct Student_type
{
char name[10];
char explanation[10];
};


int main()
{
FILE *fp;
struct Student_type stud[10];
if ((fp = fopen("test1.txt", "r")) == NULL)
{
printf("No Student Infomation Existed\n");
exit(0);
}
int i = 0;
while (! feof(fp))
{
fscanf(fp, "%s %s\n", stud[i].name, stud[i].explanation);
i ++;
};
printf("%s %s\n",stud[1].name, stud[1].explanation );


printf("*************************\n");
printf("请输入查询参数:\n");

int mark = 0;
int j;
char name[10];
scanf("%s", name);
printf("%s\n", name);
for(j=0; j<10; j++)


{
if(strcmp(stud[j].name,name) == 0)
{
printf("%s %s\n", stud[j].name, stud[j].explanation),
mark=1;
}
}
if(mark == 0)
printf("No match!\n");

fclose(fp);
return 0;

}



test1.txt中的内容

aaa  hello1
aab hello2
aac hello3
aad hello4
aae hello5
aaf hello6
aag hello7
aag hello8
aai hello9
aaj hello10

读书人网 >C++

热点推荐