比较文件中的字符串

时间:2015-03-29 19:09:35

标签: c string file stdio

我有点问题。我希望程序搜索文件,如果它与用户的输入匹配,那么程序将显示" hello"如果没有,它将显示" bye"。这些输出仅用于测试目的。回到问题所在。当它到达比较线时,程序崩溃。现在我对如何解决问题毫无头绪。我请求别人帮助我,并提前致谢。以下是代码片段:

int CaseFunc();
int CaseFunc(){ 
FILE *memFile;
char memberfname[28];
char memberlname[28];
char ffname[28];
char flname[28];
char code;
struct PersonalInfo array[1000];
int memberage;
int menuANS;
int members;
int i;
members = 0;

printf("Enter your answer here: ");
scanf(" %d", &menuANS);

switch (menuANS){
    case 1:
        system("cls");
        printf("Enter code: ");
        scanf(" %c", &code);
        do {
            printf("Enter First Name: ");
            scanf("%s", memberfname);
            printf("Enter Last Name: ");
            scanf("%s", memberlname);
            printf("Enter Age: ");
            scanf("%d", &memberage);

            if ((memFile = fopen("TheCarDepot Members.txt", "a+")) == NULL){    //If the file path is incorrect, an error message is displayed
                fprintf(stderr, "Error while opening file  (%d: %s)\n",errno, strerror(errno)); //Error message that will be displayed if file path is incorrect
            return;
        }

        fprintf (memFile, "%s %s %d", memberfname,  memberlname,    memberage); 
        fprintf (memFile, "\n");            
        fclose(memFile);
        printf("\n\n");
        printf ("*%s %s is now a member of TheCarDepot Team.*\n", memberfname, memberlname);

        printf("Enter code: ");
        scanf(" %c", &code);
    }
    while (code != 'x');
    break;

    case 2:
        if ((memFile = fopen("TheCarDepot Members.txt", "r")) == NULL){ //If the file path is incorrect, an error message is displayed
            fprintf(stderr, "Error while opening file  (%d: %s)\n",errno, strerror(errno)); //Error message that will be displayed if file path is incorrect
            return;
        }

        //while the file is opened and not at the end, the strings are stored into variables which forms an array of strings
        for (members = 0; members < 200; members++) {
           if (fscanf(memFile, "%s %s %d", memberfname, memberlname, &memberage) == 3)  //Reads the contents of the file
               break;
           array[members].fname = strdup(memberfname);
           array[members].lname = strdup(memberlname);
           array[members].age = memberage;
        }
        fclose(memFile);

        printf("Enter your first name: ");
        scanf("%s", ffname);
        printf("\n");

        if (strcmp(array[members].fname, ffname) == 0){
            printf ("hello");
        }
        else if (strcmp(array[members].fname, ffname) != 0){
            printf ("bye");
        }
        break;

3 个答案:

答案 0 :(得分:0)

您必须验证array[members].fname,如果它是NULL,或者它不包含字符串结尾字符,那么它可能会崩溃。

答案 1 :(得分:0)

for (members = 0; members < 200; members++) {
           if (fscanf(memFile, "%s %s %d", memberfname, memberlname, &memberage) == 3)  //Reads the contents of the file
           break; //exit the loop if the reading is successful, the following lines are never executed
           array[members].fname = strdup(memberfname);
           array[members].lname = strdup(memberlname);
           array[members].age = memberage;
}

基本上你手动退出循环,并且从不在结构中输入值,因此指针无效。

此外,您有内存泄漏,fnamelname应该被释放。

答案 2 :(得分:0)

当您退出for循环时,members可能等于200,这是一个未初始化的数组条目。将其传递给strcmp很容易导致崩溃。

此外,如果由于fscanf成功而提前退出循环,则您的array条目也会被缩小。再次,这可能会导致崩溃。

您需要在退出循环之前初始化array条目,并且需要检查以确保members是&lt;在访问该数组条目之前200。