1

私は C プログラミングの初心者で、要件仕様にコードを書きましたが、一貫してセグメンテーション違反が発生し、先に進むことができません。ファイル名が 'code.c' で、引数 (ファイル名) を渡さないというエラーで実行された場合。しかし、ファイル名が渡されると、セグメンテーション違反に陥ります。ヘルプ/提案をいただければ幸いです。

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



struct _data 
{               
   char *firstName;
   char *lastName;
   long number;
}; 

// SCAN FILE
int SCAN(FILE *(*stream))
{
    *stream = fopen("inputFile.data", "r");

    int ch = 0, lines = 0;

    while (!feof(*stream))
    {
        ch = fgetc(*stream);
        if (ch == '\n')
        {
            lines++;
        }
    }
    return lines;
}

// LOAD FILE
struct _data *LOAD(FILE *stream, int size) 
{
    int i;
    size_t chrCount;
    char *text, *number, *firstName, *lastName;
    struct _data *BlackBox;

    if ((BlackBox = (struct _data*)calloc(size, sizeof(struct _data))) == NULL) 
    {
          printf("ERROR - Could not allocate memory.\n");
          exit(0);
    }

    rewind(stream);


    for (i = 0; i < size; i++) 
    {
        getline(&text, &chrCount, stream);
        firstName   = strtok(text, " ");
        lastName    = strtok(text, " ");
        number      = strtok(NULL, "\n");

        // Allocate memory for name part of struct.
        if ((BlackBox[i].firstName = (char*)calloc(strlen(firstName), sizeof(char))) == NULL) 
        {
           printf("ERROR - Could not allocate memory.\n");
           exit(0);
        }
        if ((BlackBox[i].lastName = (char*)calloc(strlen(lastName), sizeof(char))) == NULL)
        {
           printf("ERROR - Could not allocate memory.\n");
           exit(0);
        }

        strcpy(BlackBox[i].firstName, firstName);
        strcpy(BlackBox[i].lastName, lastName);
        BlackBox[i].number = atol(number);
    }
    fclose(stream);
    return BlackBox;
}


void SEARCH(struct _data *BlackBox, char *name, int size, int inputs) 
{
    int i;
    int found = 0;
    char *search = " ";
    char *firstName;
    char *lastName; 

    if (inputs == 2)
    {
        firstName = strtok(name, search);
        lastName = strtok(NULL, search);
    }


    printf("*******************************************\n");
    if (inputs == 2)
    {   
        for (i = 0; i < size; i++) 
        {          
            if (!strcasecmp(firstName, BlackBox[i].firstName) && !strcasecmp(firstName, BlackBox[i].firstName))
            {
                printf("The name was found at the %d entry.\n", i);
                found = 1;
                break;
            }
        }
    }
    else
    {
        for (i = 0; i < size; i++) 
        {          
            if (!strcasecmp(firstName, BlackBox[i].firstName) || !strcasecmp(firstName, BlackBox[i].firstName))
            {
                printf("The name was found at the %d entry.\n", i);
                found = 1;
                break;
            }
        }
    }

    if (found == 0) 
    {
          printf("The name was NOT found.\n");
    }
    printf("*******************************************\n");
}

// FREE MEMORY
void FREE(struct _data *BlackBox, int size) 
{
    int i;
    for (i = 0; i < size; i++) 
    {
        free(BlackBox[i].firstName);
        free(BlackBox[i].lastName);
    } 
    free(BlackBox);
    BlackBox = NULL;
}


// MAIN
int main(int argv, char **argc) 
{
    int size;
    FILE *stream;
    struct _data *BlackBox;

    // argv == 1 WORKS, Below message is printed.
    if (argv == 1) 
    {          
        printf("*******************************************\n");
        printf("* You must include a name to search for.  *\n");
        printf("*******************************************\n");
    }
    // argv == 2 DOES NOT WORK, Segmentation Fault.     
    if (argv == 2) 
    {
        size = SCAN (&stream);
        BlackBox = LOAD(stream, size);
        SEARCH(BlackBox, argc[1], size, 1);
    }
    if (argv == 3) 
    {
        size = SCAN(&stream);
        BlackBox = LOAD(stream, size);
        SEARCH(BlackBox, argc[2], size, 2);
    }
    return 0;
}
4

3 に答える 3

0

これを試してください (ただし、Windows で Visual Studio を使用しています)。最後の行に '\n' がないかどうかをチェックするコードを追加し、可変数の検索語を許可しました。また、null 終端文字を考慮して、文字列のメモリ割り当てを 1 増やしました。を使用していることに気付きましたがgetline(const char*...、これは GNU (Linux?) だと思いますfgets()。VS でコンパイルしてテストできるように変更します (必要に応じて元に戻すことができます)。安全のために、さまざまなヌルチェックも入れました。

#include <iostream>

using namespace std;

struct _data
{
    char *firstName;
    char *lastName;
    long number;
};

// SCAN FILE
int SCAN(FILE *(*stream))
{
    *stream = fopen("inputFile.data", "r");
    if (*stream == NULL)
    {
        perror("Error opening file");
        return 0;
    }

    char ch = 0, lines = 0, linesize = 0;

    while ((ch = fgetc(*stream)) != EOF)
    {
        if (ch == '\n')
        {
            lines++;
            linesize = 0;
        }
        else linesize++;
    }

    if (linesize > 0)
        lines++; // (last line doesn't have '\n')

    return lines;
}

// LOAD FILE
struct _data *LOAD(FILE *stream, int lineCount)
{
    int i;
    size_t chrCount = 256;
    char text[256], *result, *number, *firstName, *lastName;
    struct _data *BlackBox;

    if ((BlackBox = (struct _data*)calloc(lineCount, sizeof(struct _data))) == NULL)
    {
        printf("ERROR - Could not allocate memory.\n");
        exit(0);
    }
    else memset(BlackBox, 0, sizeof(struct _data) * lineCount); // (make sure all data members are null to begin)

    rewind(stream);


    for (i = 0; i < lineCount; i++)
    {
        result = fgets(text, chrCount, stream);
        if (result == NULL)
            break; // (EOF)

        firstName = strtok(text, " ");
        lastName = strtok(NULL, " ");
        number = strtok(NULL, "\n");

        // Allocate memory for name part of struct.
        if ((BlackBox[i].firstName = (char*)calloc(strlen(firstName) + 1, sizeof(char))) == NULL)
        {
            printf("ERROR - Could not allocate memory.\n");
            exit(0);
        }
        if ((BlackBox[i].lastName = (char*)calloc(strlen(lastName) + 1, sizeof(char))) == NULL)
        {
            printf("ERROR - Could not allocate memory.\n");
            exit(0);
        }

        strcpy(BlackBox[i].firstName, firstName);
        strcpy(BlackBox[i].lastName, lastName);
        BlackBox[i].number = atol(number);
    }

    fclose(stream);

    return BlackBox;
}


void SEARCH(struct _data *BlackBox, char **names, int lineCount, int inputs)
{
    int i, l;
    int found = 0;

    printf("*******************************************\n");

    for (i = 0; i < inputs; ++i)
    {
        for (l = 0; l < lineCount; ++l)
        {
            if (BlackBox[l].firstName != NULL && !_stricmp(names[i], BlackBox[l].firstName)
                || BlackBox[l].lastName != NULL && !_stricmp(names[i], BlackBox[l].lastName))
            {
                printf("The name was found on line %d.\n", 1 + l);
                found = 1;
                break;
            }
        }
        if (found) break;
    }

    if (!found)
        printf("The name was NOT found.\n");

    printf("*******************************************\n");
}

// FREE MEMORY
void FREE(struct _data *BlackBox, int lineCount)
{
    int i;
    for (i = 0; i < lineCount; i++)
    {
        if (BlackBox[i].firstName != NULL)
            free(BlackBox[i].firstName);
        if (BlackBox[i].lastName != NULL)
            free(BlackBox[i].lastName);
    }
    free(BlackBox);
}


// MAIN
int main(int argc, char **argv)
{
    int lineCount;
    FILE *stream;
    struct _data *BlackBox;

    // argc == 1 WORKS, Below message is printed.
    if (argc == 1)
    {
        printf("*******************************************\n");
        printf("* You must include a name to search for.  *\n");
        printf("*******************************************\n");
    }
    // argc == 2 DOES NOT WORK, Segmentation Fault.     
    if (argc > 1)
    {
        lineCount = SCAN(&stream);
        if (lineCount > 0)
        {
            BlackBox = LOAD(stream, lineCount);
            SEARCH(BlackBox, argv + 1, lineCount, argc - 1);
            FREE(BlackBox, lineCount);
        }
    }
    return 0;
}

コマンドラインでテストしたところ、動作しました。

于 2015-04-09T07:14:40.337 に答える
0

このコードに問題があります:

    firstName   = strtok(text, " ");
    lastName    = strtok(text, " ");
    number      = strtok(NULL, "\n");

...

    BlackBox[i].number = atol(number);

2 番目の呼び出しは、最初の引数としてstrtok()渡す必要があります。そのままでは、最初の呼び出しが 2 番目の呼び出しがすべてを消費するように変更されるためNULL、3 番目のstrtok()呼び出しは確実に返されます (誤って最初から再度トークン化する場合)。ただし、それをテストしないと、null ポインターを逆参照しようとします。NULLtextatol()

更新

さらに、@chqrlie およびその後の @JamesWilkins が観察したように、文字列ターミネータにもスペースが必要なためBlackBox[i].firstName、 andに十分なスペースを割り当てません。これは、セグメンテーション違反をBlackBox[i].lastName引き起こす可能性がある、まったく別の問題です。に切り替えるという@chqrlieの提案が好きですが、各割り当てを1単位増やすだけで十分です。strdup()

更新 2 :

さらに、次の行に問題があります。

    getline(&text, &chrCount, stream);

最初の呼び出しの前に変数を初期化しないtextため、ジャンク値が含まれています。この関数は、最初の引数が NULL ポインターを指している場合にのみバッファーを割り当てます。それ以外の場合は、最初の引数を逆参照して取得したポインターが指すバッファーに行を書き込みます。メモリ内のランダムな場所に書き込むと、未定義の動作が確実に発生し、実際にはセグメンテーション違反として現れることがよくあります。

さらに、ファイルのどの行も最初の行より長くないと信頼できない場合を除き、text各ループ反復の終わりにポインターを解放し、その値を にリセットして、次の反復で新しいバッファーNULLを割り当てる必要もあります。getline()反復ごとに解放しない場合は、代わりにループの終了後に解放する必要があります。そうしないと、メモリリークが発生します。

于 2015-04-08T22:23:57.140 に答える