-1

嘿,我的任务是从由空格分隔的文件中读取数据并对数据进行排序。我不断收到段错误,我无法弄清楚我的代码有什么问题。谢谢您的帮助。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LINE 100
#define MAX_NAME 30
int countLinesInFile(FILE* fptr);
int findPlayerByName(char **names, char* target, int size);
int findMVP(int* goals, int* assists, int size);
void printPlayers(int* goals, int* assists, char** names, int size);
void allocateMemory(int **goals, int** assists, char*** names, int size);
void sortPlayersByGoals(int* goals, int* assists, char** names, int size);
void writeToFile(FILE *fptr, int* goals, int* assists, char** names, int size);
void readLinesFromFile(FILE* fptr, int* goals, int* assists, char** names, int        numLines);

int main(int argc, char **argv)
{
    FILE *fptr;
    int size;
    int* goals;
    int* assists;
    char** names;
    if(argc != 2)
    {
            printf("Invalid number of arguments\n");
            return 1;
    }
    fptr = fopen(argv[1], "r");
    if(fptr == NULL)
    {
            printf("Failed to open input file.\n");
            return 2;
    }
    size = countLinesInFile(fptr);
    allocateMemory(&goals, &assists, &names, size);
    int numLines;
    numLines = size;
    readLinesFromFile(fptr, goals, assists, names, numLines);
    printPlayers(goals, assists, names, size);

    return 3;
}
void printPlayers(int* goals, int* assists, char** names, int size)
{
    int i;
    for(i = 0; i < size; i++)
    {
    printf("%c     %d     %d", *(names + i), *(goals + i), *(assists + i));
    }

}
void readLinesFromFile(FILE* fptr, int* goals, int* assists, char** names, int numLines)
{
    char line[MAX_LINE];
    int i;
    char* value;

    for(i = 0; i < numLines; i++)
    {
            fgets(line, MAX_LINE, fptr);
            value = strtok(line, " ");
            *(*names + i) = atoi(value);
            value = strtok(line, " ");
            *(goals + i) = atoi(value);
            value = strtok(line, " ");
            *(assists + i) = atoi(value);
    }


}
void allocateMemory(int **goals, int** assists, char*** names, int size)
{

    *goals = malloc(sizeof(int)* size);
    *assists = malloc(sizeof(int)* size);
    *names = malloc(sizeof(char*)* MAX_NAME);

}
int countLinesInFile(FILE* fptr)
{
    int i;
    char line[MAX_LINE];
    i = 0;
    while(fgets(line, MAX_LINE, fptr) != NULL)
    {
    i++;
    }
    rewind(fptr);
    return i;
}

- 插入 -

输入文件看起来像: Redden 2 0 Berglund 5 2 Jackman 2 0 Stewart 4 0 Oshie 3 5 McDonald 2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5

4

2 回答 2

0

在 allocateMemory() 中,每个*names都是一个char **. 你需要分配两次

*names = malloc(...);
for (<each pointer in names>) {
    <pointer> = malloc(...);
}

不要忘记释放两次

for (<each pointer in names>) {
    free(<pointer>);
}
free(*names);
于 2013-02-20T21:44:39.283 回答
0

关于您的分段错误,您正在取消引用指针**names,但*names它是一个未初始化的指针。这会导致未定义的行为,因此在您的情况下会出现执行错误(并且可能是最糟糕的!)。要解决它,您必须分配一个二维数组,例如:

*a = malloc(numLines * sizeof **a);
for (i = 0; i < n; i++) (*a)[i] = malloc(NAME);

此外,您的strtok使用似乎是错误的。这个函数保持一个指向你的数据的静态指针(因此它通常不是线程安全的):你应该NULL 在下一次调用中使用这个值。否则,它将每次返回相同的地址。man strtok通过输入您最喜欢的网络搜索引擎来查看文档。

char line[MAX_LINE];
int i;

for (i = 0; i < numLines; i++)
{
    fgets(line, sizeof line, fptr);
    value = strtok(line, " ");
    (*names)[i] = atoi(value); /* This assignment looks strange. */
    value = strtok(NULL, " ");
    goals[i] = atoi(value);
    value = strtok(NULL, " ");
    assits[i] = atoi(value);
}

不要忘记数组下标运算符!如果是真实世界的程序,sscanf看起来更合适。

于 2013-02-20T21:46:29.797 回答