2016-11-18 66 views
0

我在第一個printf,printf(「Original:\ n」)後立即收到分段錯誤;注意:grades.csv是一個excel文件。帶功能的分段錯誤

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <string.h> 




void getGradesFromFile(const char *filename, 
          int size, 
          char line[], 
       int studentI_D[], 
       int test1, 
       int test2, 
       int test3, 
       char grades[]); 



void insertionSort(int length, 
        int studentI_D[], 
      char list[]); 

void flushScanf(); 

void binarySearch(int size, 
       int target, 
     int studentID[]); 

void printArray(int size, 
     char A[*], 
     int B[*], 
     int test1, 
     int test2, 
     int test3, 
     char grades[]); 


int main(void) 
{ 
    char grades[2]; 
    int size = 60; 
    char str[size]; 
    int studentID [7]; 
    char letter; 
    int exam1, exam2, exam3; 
    int num; 

    printf("Original:\n"); 
    getGradesFromFile("grades.csv", size, str, studentID, exam1, exam2, exam3, grades);// function call 
    printArray(size, str, studentID, exam1, exam2, exam3, grades); 

    printf("Sorted:"); 
    insertionSort(size, studentID, grades);// inscertion sort function call 
    printf("\n\n"); 
    printf("Enter student ID and -1 to quit"); 
    scanf("%d", num); 
    flushScanf(); 

printf("Hello World"); 
    while (num != -1){ 
    if (num > 99999 && num < 1000000){ 
    binarySearch(size, num, studentID); 


} 
    else { 
printf ("Error and enter again [100000, 999999] and -1 to quit"); 
} 
exit; 
    } 




    return 0; 
} // end of main 
/*    
*Function name: getNumbersFromFile 
* 
*Input Parameters: const char *filename, int size, float number[] 
* 
*Desription: gets numbers from the file and checks for errors 
* 
*Return value: filename 
*/ 

我不確定使用此功能是否正確獲取數據,請欣賞此功能的任何幫助。該函數的作用是從Excel文件中獲取數據並將其放入字符串中。

void getGradesFromFile(const char *filename, 
       int size, 
          char line[], 
          int studentI_D[], 
          int test1, 
       int test2, 
          int test3, 
          char grades[]) 
{ 
    FILE* pData = fopen("grades.csv","r+"); 
    int i = 0; 
    if (pData == NULL) {// opens file grades.csv 
    fprintf(stderr, "Error opening file.\n", filename);// error handling statement 
    exit(1); 
    } 


    while (fgets(line,sizeof(line), pData) != NULL){ 
sscanf(line, "%s, %d, %d, %d, %s", studentI_D[i], &test1, &test2, &test3, 
grades[i]); 
i++; 


} 


    if (fclose(pData)== EOF) { 
    fprintf(stderr, "Error closing file.\n", filename);// close file 
    exit(2); 
    } 
} 


void insertionSort(int length, int studentID[], char grades[]) 
{ 
    int i, key, key1, j; 
    for (i = 1; i < length; i++) 
    { 
     key = studentID[i]; 
     key1= grades[i]; 
     j = i-1; 

     while (j >= 0 && studentID[j] > key) 
     { 
      studentID[j+1] = studentID[j]; 
      grades[j+1] = grades[j]; 
      j = j-1; 
     } 
     studentID[j+1] = key; 
     grades[i+j] = key1; 
    } 

} 

void flushScanf() 
{ 
    while(getchar() != '\n') 
     ; 
} 



void binarySearch(int size, int target, int student[]) 
{ 
    char *locn; 
    int first = 0; 
    int last = size - 1; 
    int mid; 
    bool found = false; 

    while (first <= last) { 
     mid = (first + last)/2; 
    if (target > student[mid]) { 
     first = mid + 1; 
    } else if (target < student[mid]) { 
    last = mid - 1; 
    } else { 
    *locn = mid; 
    found = true; 
    break; 
    } 
    } 

} 




void printArray(int size, 
       char A[size], 
       int B[], 
       int test1, 
       int test2, 
       int test3, 
       char grades[]) 
{ 

    for (int j=0; j<4; j++) { 
    printf("%s, %d, %d, %d, %s", A[j], B[j], test1, test2, test3, grades[j]); 
    } 

} 
+2

'的sizeof(線)' - >'size','studentI_D ​​[I]' - >'studentI_D ​​+ i','%D'用於'studentI_D' – BLUEPIXY

+4

據透露,'退出;'是極有可能*不會*做你認爲會的事。打開你的警告*迂腐*級別,把它們當作錯誤,並修復*標記的所有*。 [實施例](http://pastebin.com/8FcZwvLn)。 – WhozCraig

+0

另外,它實際上並沒有從文件中讀取。 – BLUEPIXY

回答

0

在getGradesFromFile中檢查您的sscnaf邏輯。你希望使用%s輸入等級,而不是%c。

void getGradesFromFile(const char *filename, 
       int size, 
          char line[], 
          int studentI_D[], 
          int test1, 
       int test2, 
          int test3, 
          char grades[]) 
{ 
    FILE* pData = fopen("grades.csv","r+"); 
    int i = 0; 
    if (pData == NULL) {// opens file grades.csv 
    fprintf(stderr, "Error opening file.\n", filename);// error handling statement 
    exit(1); 
    } 


    while (fgets(line,sizeof(line), pData) != NULL){ 
sscanf(line, "%d, %d, %d, %d, %c", &studentI_D[i], &test1, &test2, &test3, 
&grades[i]); 
i++; 

}