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