2016-12-04 55 views
0

在這個程序中,我想讀一個records.ssv的文件並使用結構數組打印出來。但是在打印數組之前要計算平均值。該文件包含學生姓名,身份證,考試1,考試2,項目1,項目2,平均和成績(字母,單個)。打印一個結構數組

當前在打印出eh數組和計算平均值時出現問題。我將發佈我的代碼並在下面輸出。

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

typedef struct { 
     char name[25]; 
     int I_D[9]; 
     int exam[2]; 
     int project[2]; 
     char grade; 
     float avg; 
} STUDENT; 

void printStuAry(int size, STUDENT stuAry[]); 

int main(void) 
{ 
    STUDENT stuAry[5]; 

    FILE* f = fopen("records.ssv", "r"); 
    if (f == NULL) { 
      printf("Error opening file records.ssv.\n"); 
      exit(1); 
    } 

    char line[65]; 
    int ind = 0; 

    while (fgets(line, sizeof(line), f) != NULL) { 
     sscanf(line, "%25[^,] ; %d ; %d ; %d ; %d ; %d ; %c", 
       stuAry[ind].name, 
       &stuAry[ind].I_D, 
       &stuAry[ind].exam[0], 
       &stuAry[ind].exam[1], 
       &stuAry[ind].project[0], 
       &stuAry[ind].project[1], 
       &stuAry[ind].grade); 
     ind++; 
    } 
    // calculates average 
    stuAry[ind].avg = (stuAry[ind].exam[0] + stuAry[ind].exam[1] + 
         stuAry[ind].project[0] + stuAry[ind].project[1]) /4.0; 

     printf("\t+---------+-------+-------+-------+---------+----------+----------+------+\n"); 
    printf("\t| Index |Student| ID | Exam1 | Exam2 |Project1 | Project2 | Grade|\n"); 
    printf("\t+---------+-------+-------+-------+---------+----------+--------+--------+\n"); 

    printStuAry(5, stuAry); 
    if (fclose(f) == EOF) { 
     printf("Error closing file records.ssv.\n"); 
     exit(2); 
    } 

    int letter; 
    printf("Enter grade letter to search and q to quit"); 
    scanf("%d", letter); 

    while (letter != -1){// tests for whether user wants to end program 
     printStuAry(5, stuAry); 
    } 

    return 0; 
} 

/* 
*Function name: printStuAry 
* 
*Input Parameters: int size Student Ary 
* 
*Desription: prints out student Ary 
*                 
*Return value: 0 
*/ 

void printStuAry(int size, STUDENT stuAry[]) 
{ 
     for (int i=0; i<size; i++) { 
       printf("\t| %d  | %s | %d | %d | %d  | %d | %d  | %c |\n", //displays student information 
             i, stuAry[i].name, stuAry[i].exam[0], 
             stuAry[i].exam[1], stuAry[i].project[0], 
             stuAry[i].project[1], stuAry[i].avg, 
             stuAry[i].grade); 
     } 
} 

輸出

 +---------+-------+-------+-------+---------+----------+----------+------+ 
     | Index |Student| ID | Exam1 | Exam2 |Project1 | Project2 | Grade| 
     +---------+-------+-------+-------+---------+----------+--------+--------+ 
     | 0  | Panzer | -544760936 | 32568 | -544762280  | 32568 | -27  |   | 
     | 1  | Basler | -1718237240 | 32767 | -544747128  | 32568 | -104  |   | 
     | 2  | Leaton | -1718237400 | 32767 | 118  | 0 | 9  |   | 
     | 3  | Bishop | 4195301 | 0 | 194  | 0 | 62  |   | 
     | 4  | Lucey | -1718237064 | 32767 | 4197349  | 0 | -96  |   | 
Enter grade letter to search and q to quit 

正確的輸出應該是什麼:

原文:

+-------------------------+--------------+------+------+---------+---------+-------+-----+ 
| Student Name|Identification|Exam 1|Exam 2|Project 1|Project 1|Average|Grade| 
+-------------------------+--------------+------+------+---------+---------+-------+-----+ 
| Holtkamp, Norman| N21102485| 83| 61| 62| 78| 71.00| C| 
| Bellomy, Shavonda| N94185259| 74| 96| 80| 98| 87.00| B| 
| Clutter, Loris| N68760306| 83| 68| 93| 70| 78.50| C| 
| Rountree, Edythe| N76813896| 98| 91| 90| 81| 90.00| A| 
| Waldeck, Marylee| N44293872| 88| 100| 70| 87| 86.25| B| 
+-------------------------+--------------+------+------+---------+---------+-------+-----+ 
+0

請妥善不知疲倦更換sscanf(line, "%25[^,] ; %d ; %d ; %d ; %d ; %d ; %c", nt你的代碼。事實上,這讓閱讀變得更加困難。 – e0k

+0

這一切都不清楚?你是否聲稱'%c'打印了你一個數字而不是一個字符?無論哪種方式可能是結構中錯誤的值。注意,這也不像你在你的函數中打印ID。另外爲什麼是整數ID數組而不是字符串(字符數組)?更何況你只是讀取它的第一個單元格的值(I_D [0]),而不是每個數字,因爲你提到%d到sscanf –

+0

另外,我建議檢查你的'sscanf()'的返回值確保所有內容都被正確讀取。 – e0k

回答

0

嘗試用sscanf(line, "%25[^,] %d %d %d %d %d %c",

+0

但是文件數據是用分號分隔的,想想如果我在sscanf中使用分號,它會更準確 – josmar

+0

在這種情況下,您需要將行讀入變量並使用strtok方法按分隔符分割。請參閱http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – Sourabh