C 无法理解为什么程序在两次while循环迭代后崩溃?

C 无法理解为什么程序在两次while循环迭代后崩溃?,c,text-files,C,Text Files,我正在编写下面的程序来解析文本文件(附件),但经过几次while循环迭代后,它一直崩溃,或者似乎存储文件内容的缓冲区被破坏了 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> char * pick_question(char *, char, int); char * print_answer(char *, char, int); int

我正在编写下面的程序来解析文本文件(附件),但经过几次while循环迭代后,它一直崩溃,或者似乎存储文件内容的缓冲区被破坏了

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

char * pick_question(char *, char, int);
char * print_answer(char *, char, int);
int no_of_questions(char*, char);
void clear();
int debug = 0;

int main(int argc, char* argv[])
{
    system("cmd /c chcp 1252");
//    system("cmd /c chcp 65001");

    if (argc < 2)
    {
        perror("Please enter a filename!\n");
        exit(0);
    }
    if (argc > 2)
    {
        debug = atoi(argv[2]);
    }

    char const* const fileName = argv[1];
    FILE* file = fopen(fileName, "r");

    if (!file)
    {
        perror("Unable to read file!\n");
        exit(0);
    }
    else
    {
        if (debug == 1)
        {
            printf("File opened successfully\n");
        }
    }

    static char *buffer;
    int fileSz;

    fseek(file, 0, SEEK_END);
    fileSz = ftell(file);
    fseek(file, 0, SEEK_SET);
    buffer = (char*) malloc((fileSz + 1) * sizeof(char));

    if (!buffer)
    {
        perror("Unable to allocate buffer!");
        exit(0);
    }

    fread(buffer, sizeof(char), fileSz, file);

    while (1)
    {
        time_t t;
        srand((unsigned) time(&t));
        int sub = rand() % 5 + 1;
        char del;
        switch (sub)
        {
        case 1:
            del = 'A';
            break;
        case 2:
            del = 'B';
            break;
        case 3:
            del = 'C';
            break;
        case 4:
            del = 'D';
            break;
        case 5:
            del = 'E';
        }

        int nrOfQues = no_of_questions(buffer, del);
        if (nrOfQues == 0)
        {
            perror("main(): no_of_questions() returned 0. Unsupported text structure in file or incorrect file encoding!");
            fclose(file);
            exit(0);
        }
        int qNo = rand() % nrOfQues + 1;
        char *ques = pick_question(buffer, del, qNo);
        if (ques)
        {
            printf("\n\n");
            puts(ques);
            printf("\n\n");
        }
        else
        {
            perror("main(): pick_question() returned NULL. Unsupported text structure in file!");
            fclose(file);
            exit(0);
        }
        printf("\n\n");
        printf("Do you want to see the answer(y/n)?");
        char ans, repeat;
        scanf("%c", &ans);
        if ( ans == 'Y' || ans == 'y')
        {
            char *ans = print_answer(buffer, del, qNo);
            if (ans)
            {
                printf("\n\n");
                puts(ans);
                printf("\n\n");
            }
            else
            {
                printf("\n\n");
                perror("main(): print_answer() returned NULL. Unsupported text structure in file!");
                fclose(file);
                exit(0);
            }
        }
        printf("Do you want to try more questions (y/n)?");
        clear();
        scanf("%c", &repeat);
        if (repeat == 'N' || repeat == 'n')
        {
            break;
        }
        clear();
    }
    printf("\n\n");
    printf("********  Thank you for using TULE Master! ********");
    printf("\n\n");
    fclose(file);
    return 0;
}

char * pick_question(char * buffer, char sub, int qNo)
{
    char tmpBuff[20];
    char tmpBuff2[20];
    const char * searchStr = "FRÅGA";
    const char * searchStr2 = "A 1 SVAR:";
    const char * searchStr3 = "*****************************************";
    char *pStr, *currPos, *nStr, *tmpStr, *tmpStr2;
    currPos = buffer;

    int count = snprintf(tmpBuff, 20, "FRÅGA %c %d", sub, qNo);

    if (count >= 0 || count < 20)
    {
        if (debug)
        {
            printf("tmpBuff is %s\n", tmpBuff);
        }
        currPos = strstr(currPos, tmpBuff);
        if (currPos)
        {
            pStr = currPos;
            nStr = currPos + 1;
            nStr = strstr(nStr, searchStr);
            if (!nStr)
            {
                nStr = currPos;
                nStr = strstr(nStr, searchStr2);
                if (!nStr)
                {
                    printf("pick_qestion(): nStr is NULL. Unsupported "
                           "text structure");
                    return NULL;
                }
            }
            // Check if it is a scenario based question
            count = snprintf(tmpBuff2, 20, "FRÅGA %c %d", sub, qNo-1);
            if (count >= 0 || count < 20)
            {
                tmpStr = strstr(buffer, tmpBuff2);
                tmpStr2 = strstr(tmpStr, searchStr3);
                if (tmpStr < tmpStr2 && tmpStr2 < pStr)
                {
                    pStr = tmpStr2;
                }
            }
            int qLen = nStr - pStr;
            char *ques = malloc(sizeof(char) * (qLen+1));
            snprintf(ques,qLen,"%s", pStr);
            return ques;
        }
        else
        {
            printf("pick_qestion(): string \"FRÅGA\" not found in file!");
            return NULL;
        }
    }
    printf("pick_qestion(): snprintf was not successful!");
    return NULL;
}

char * print_answer(char * buffer, char sub, int qNo)
{
    char tmpBuff[20];
    char *pStr, *currPos, *nStr;

    int count = snprintf(tmpBuff, 20, "%c %d SVAR:", sub, qNo);

    if (count >= 0 || count < 20)
    {
        currPos = strstr(buffer, tmpBuff);
        if (!currPos)
        {
            printf("print_answer(): string \"SVAR\" not found in file!");
        }
        pStr = currPos;
        nStr = currPos + 1;
        char tmpBuff2[20];
        int count = snprintf(tmpBuff2, 20, "%c %d SVAR:", sub, qNo+1);
        if (count < 0 || count >= 20)
        {
            printf("print_answer(): snprint was not successful!");
            return NULL;
        }
        nStr = strstr(nStr, tmpBuff2);
        if (!nStr)
        {
            nStr = buffer + strlen(buffer);
        }
        int ansLen = nStr - pStr;
        char *ans = malloc(sizeof(char) * (ansLen+1));
        snprintf(ans, ansLen, "%s", pStr);
        return ans;
    }
    printf("print_answer(): snprint was not successful!");
    return NULL;
}

int no_of_questions(char *buffer, char sub)
{
    char tmpBuff[20];
    char *currPos, *pStr;

    int count = snprintf(tmpBuff, 20, "FRÅGA %c", sub);

    if (count >= 0 || count < 20)
    {
        if (debug)
        {
            printf("tmpBuff is %s\n", tmpBuff);
        }
        currPos = strstr(buffer, tmpBuff);
        while (currPos != NULL)
        {
            pStr = currPos;
            currPos = currPos + 1;
            currPos = strstr(currPos, tmpBuff);
        }
        if (pStr != buffer)
        {
            pStr += 9;
            char tmpBuff2[20];
            memcpy(tmpBuff2, pStr, 2);
            if (debug)
            {
                printf("No. of questions for %c DEL is are %d\n", sub,
                       atoi(tmpBuff2));
            }
            return atoi(tmpBuff2);
        }
        return 0;
    }
    return 0;
}
void clear()
{
    int c;
    while ((c = getchar()) != '\n' && c != EOF) { }
}
#包括
#包括
#包括
#包括
char*选择问题(char*,char,int);
字符*打印答案(字符*,字符,整数);
问题的整数编号(字符*,字符);
无效清除();
int-debug=0;
int main(int argc,char*argv[])
{
系统(“cmd/c chcp 1252”);
//系统(“cmd/c chcp 65001”);
如果(argc<2)
{
perror(“请输入文件名!\n”);
出口(0);
}
如果(argc>2)
{
debug=atoi(argv[2]);
}
char const*const fileName=argv[1];
FILE*FILE=fopen(文件名,“r”);
如果(!文件)
{
perror(“无法读取文件!\n”);
出口(0);
}
其他的
{
如果(调试==1)
{
printf(“文件已成功打开\n”);
}
}
静态字符*缓冲区;
int-fileSz;
fseek(文件,0,SEEK_END);
fileSz=ftell(文件);
fseek(文件,0,搜索集);
buffer=(char*)malloc((fileSz+1)*sizeof(char));
如果(!缓冲区)
{
perror(“无法分配缓冲区!”);
出口(0);
}
fread(buffer,sizeof(char),fileSz,file);
而(1)
{
时间;
srand((未签名)时间(&t));
int sub=rand()%5+1;
查尔德尔;
开关(sub)
{
案例1:
del='A';
打破
案例2:
del='B';
打破
案例3:
del='C';
打破
案例4:
del='D';
打破
案例5:
del='E';
}
int nrOfQues=没有问题(缓冲区,del);
如果(nrOfQues==0)
{
perror(“main():没有任何问题()返回0。文件中的文本结构不受支持或文件编码不正确!”);
fclose(文件);
出口(0);
}
int qNo=rand()%nrOfQues+1;
char*ques=选择问题(缓冲区、del、qNo);
如果(问题)
{
printf(“\n\n”);
售股权证;
printf(“\n\n”);
}
其他的
{
perror(“main():pick_question()返回NULL。文件中不支持的文本结构!”);
fclose(文件);
出口(0);
}
printf(“\n\n”);
printf(“您想看到答案(是/否)?”;
char ans,重复;
scanf(“%c”和“&ans”);
if(ans=='Y'| | ans=='Y')
{
char*ans=打印回答(缓冲区、del、qNo);
如果(ans)
{
printf(“\n\n”);
出售(ans);
printf(“\n\n”);
}
其他的
{
printf(“\n\n”);
perror(“main():print_answer()返回NULL。文件中不支持的文本结构!”);
fclose(文件);
出口(0);
}
}
printf(“您想尝试更多问题(是/否)?”;
清除();
scanf(“%c”,重复(&R));
如果(重复=='N'| |重复=='N')
{
打破
}
清除();
}
printf(“\n\n”);
printf(“*******感谢您使用TULE Master!*******”);
printf(“\n\n”);
fclose(文件);
返回0;
}
字符*选取问题(字符*缓冲区、字符子区、整数qNo)
{
char-tmpBuff[20];
煤焦tmpBuff2[20];
const char*searchStr=“FRÅGA”;
const char*searchStr2=“A 1 SVAR:”;
const char*searchStr3=“*************************************************”;
字符*pStr、*currPos、*nStr、*tmpStr、*tmpStr2;
currPos=缓冲区;
int count=snprintf(tmpBuff,20,“FRÅGA%c%d”,sub,qNo);
如果(计数>=0 | |计数<20)
{
如果(调试)
{
printf(“tmpBuff是%s\n”,tmpBuff);
}
currPos=strstr(currPos,tmpBuff);
if(currPos)
{
pStr=currPos;
nStr=当前位置+1;
nStr=STRSTRSTR(nStr,搜索STR);
如果(!nStr)
{
nStr=currPos;
nStr=strstr(nStr,搜索STR2);
如果(!nStr)
{
printf(“pick_qestion():nStr为空。不受支持”
“文本结构”);
返回NULL;
}
}
//检查是否是基于场景的问题
计数=snprintf(tmpBuff2,20,“FRÅGA%c%d”,sub,qNo-1);
如果(计数>=0 | |计数<20)
{
tmpStr=strstr(缓冲器,tmpBuff2);
tmpStr2=strstrstr(tmpStr,searchStr3);
如果(tmpStr=0 | |计数<20)
{
currPos=strstr(缓冲器,tmpBuff);
如果(!currPos)
{
printf(“print_answer():在文件中找不到字符串\“SVAR\”);
}
pStr=currPos;
nStr=当前位置+1;
煤焦tmpBuff2[20];
int count=snprintf(tmpBuff2,20,“%c%d SVAR:”,sub,qNo+1);
如果(计数<0 | |计数>=20)
{
printf(“print_answer():snprint未成功!”);
返回NULL;
}
nStr=strstr(nStr,tmpBuff2)