2013-02-20 93 views
-1

嘿,我的任務是從一個空格分隔的文件讀取數據並對數據進行排序。我一直在收到seg錯誤,我無法弄清楚我的代碼有什麼問題。謝謝您的幫助。fgets()strtok()

#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; 
} 

- INSERT -

輸入文件看起來像: 雷登2 0 伯格倫德5 2 傑克曼2 0 斯圖爾特4 0 Oshie 3 5 麥當勞2 4 Pietrangelo 2 7 門階2 6 塔拉先科5 5

+1

把printf放在你的代碼中給我們一個關於它在哪裏爆炸的基本概念。或者張貼輸入以及我們可以自己測試:) – alvonellos 2013-02-20 21:40:32

+1

哇! A [三星級程序員](http://c2.com/cgi/wiki?ThreeStarProgrammer):) – pmg 2013-02-20 21:41:35

+0

輸入文件是: 雷登2 0 伯格倫德5 2 傑克曼2 0 斯圖爾特4 0 Oshie 3 5 麥當勞2 4 Pietrangelo 2 7 Perron 2 6 Tarasenko 5 5 – user2093120 2013-02-20 21:44:35

回答

0

在allocateMemory()中,每個*nameschar **。您需要分配兩次

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

不要忘記釋放兩次太

for (<each pointer in names>) { 
    free(<pointer>); 
} 
free(*names); 
0

關於您的分段錯誤,你解引用指針**names,但*names是一個未初始化的指針。這導致了一個未定義的行爲,因此在你的情況下執行錯誤(它可能是最糟糕的!)。爲了解決這個問題,你必須分配雙dimensionnal陣列,例如:

*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看起來更合適。

+0

我必須使用fgets和strtok,它的類作業 – user2093120 2013-02-20 22:00:09

+0

@ user2093120:在這種情況下,不要忘記檢查文檔。祝你好運! ;-) – md5 2013-02-20 22:13:21

+0

至於看起來很奇怪的那條線,你會如何改變它?我得到了正確閱讀目標和助攻的程序,但它一直給我垃圾名稱 – user2093120 2013-02-20 22:17:43