嘿,我的任务是从一个用空格分隔的文件中读取数据并对数据进行排序。我一直有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 argumentsn");
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 0Berglund 5 2杰克曼2 0斯图尔特4 0Oshie 3 5麦当劳2 4Pietrangelo 2 7Perron 2 6Tarasenko 5 5
在allocateMemory()中,每个*names
都是一个char **
。您需要分配两次
*names = malloc(...);
for (<each pointer in names>) {
<pointer> = malloc(...);
}
别忘了免费两次太
for (<each pointer in names>) {
free(<pointer>);
}
free(*names);
关于分段错误,您正在取消引用指针**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
看起来更合适。