有大神能看下哪里出问题了?每次都是这样,已经打击到我的自信了。

c_program_learner 2020-11-04 02:55:15
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<time.h>
#define CODE 0x3b
#define MAXLEN 100
typedef struct
{
char *english;
char *chinese;
}Word;
Word word[MAXLEN];
int testType=0;
int init()
{
FILE *fp;
char fname[50];
char line[MAXLEN];
char *p;
char *word1;
char *word2;
int i=0;
int j=0;
int len;
printf("\t welcome to the words test stuition\n");
printf("============================================\n");
printf("please input the fname:");
scanf("%s",fname);
if((fp=fopen(fname,"r"))==NULL)
{
printf("opened error");
exit(0);
}
fflush(stdin);
while(fgets(line,MAXLEN,fp))
{
for(j=0;j<strlen(line);j++)
{
if(line[j]!='\n')
{
line[j]=line[j]^CODE;
}
}
p=strchr(line,'\n');
if(p!=NULL)
{
*p='\0';
}
p=strchr(line,'=');
word1=line;
word2=p;
*p='\0';
++word2;
len=strlen(word1);
word[i].english=(char*)malloc(len+1);
strcpy(word[i].english,word1);
len=strlen(word2);
word[i].chinese=(char*)malloc(len+1);
strcpy(word[i].chinese,word2);
i++;
}
fclose(fp);
return i;
}
void endtest(int size)
{
int i;
for(i=0;i<size;i++)
{
free(word[i].english);
word[i].english=NULL;
free(word[i].chinese);
word[i].chinese=NULL;
}
}
void test(int size)
{
int i,j,t;
int flag;
char choice;
int xm[4];
int count=0;
int score=0;
printf("please choice;1---chinese to eng0-----eng to chin");
scanf("%d",&testType);
fflush(stdin);

for(i=0;i<size;i++)
{
if(!testType)
{
printf("%d.english--%s--means:\n",i+1,word[i].english);
}
else
{printf("%d.chin--%s--means:\n",i+1,word[i].chinese);
}
srand(time(NULL));
count=0;
while(count<=4)
{
t=rand()%size;
if(t==i)
{
continue;
}
flag=0;
for(j=0;j<count;j++)
{
if(xm[j]==t)
{
flag=1;
}
}
if(!flag)
{
xm[count]=t;
count++;
}
}
t=rand()%4;
xm[t]=i;
if(!testType)
{
printf("\nA.%s\n",word[xm[0]].chinese);
printf("\nB.%s\n",word[xm[1]].chinese);
printf("\nC.%s\n",word[xm[2]].chinese);
printf("\nD.%s\n",word[xm[3]].chinese);
}
else
{
printf("\tA.%s\n",word[xm[0]].english);
printf("\tB.%s\n",word[xm[1]].english);
printf("\tC.%s\n",word[xm[2]].english);
printf("\tD.%s\n",word[xm[3]].english);
}
printf("please input your choice:");
choice=getchar();
fflush(stdin);
if(choice>='a'&&choice<='z')
{
choice=choice-'a'+'A';
}
if(choice-'A'==t)
{
printf("sorry,you are wromg");
score++;
}
else
{
printf("sorry,you are worng");
printf("the correct answer is:%c.\n",'A'+t);
}
getchar();
}
printf("the game is over. amoutis %d. correct is %d\n\nwelcome next time\n",size,score);
}
int main(void)
{
int size=init();
test(size);
getchar();
return 0;
}
...全文
3580 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mouse_zhu 2020-11-05
  • 打赏
  • 举报
回复
GNU gdb (GDB) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-pc-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from aa... (gdb) r Starting program: /tmp/aa welcome to the words test stuition ============================================ please input the fname:a.c Program received signal SIGSEGV, Segmentation fault. 0x00005555555553d2 in init () at a.c:61 61 *p='\0'; (gdb) bt #0 0x00005555555553d2 in init () at a.c:61 #1 0x0000555555555975 in main () at a.c:168 (gdb) q
glen30 2020-11-04
  • 打赏
  • 举报
回复
p=strchr(line,'=');
word1=line;
word2=p;
*p='\0';

这里p为NULL(没有找到=的时候),导致崩溃的

2,161

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 UNIX文化
社区管理员
  • UNIX文化社区
  • 文天大人
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧