嘿,我的任务是读取由空格分隔的文件中的数据并对数据进行排序。我一直遇到一个seg错误,我无法弄清楚我的代码是什么问题。谢谢你的帮助。
#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 杰克曼2 0 斯图尔特4 0 Oshie 3 5 麦当劳2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5
答案 0 :(得分:0)
在allocateMemory()中,每个*names
都是char **
。你需要分配两次
*names = malloc(...);
for (<each pointer in names>) {
<pointer> = malloc(...);
}
别忘了两次免费
for (<each pointer in names>) {
free(<pointer>);
}
free(*names);
答案 1 :(得分: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
看起来更合适。