2013-12-09 102 views
-3

任何人都知道如何將文本文件讀入結構數組?我一直在試圖弄清楚如何做到無濟於事。將文件寫入結構數組

這裏的函數頭

int getRawData(FILE* fp, struct nameRecord records[], int currSize) 

其中第一參數傳遞的文件已經開放的閱讀,nameRecord結構的第二陣列,並且記錄目前該陣列中的第三數量。該函數應該將文件中的數據讀入數組,並將其放在數組的末尾。然後它在讀取文件後返回數組中的記錄總數。

我在初始化nameRecord結構數組的元素數量時也處於虧損狀態。我們從來沒有被教過內存分配,這個問題沒有提到文件中有多少記錄,這使得初始化成爲一種令人沮喪的練習。到目前爲止,我在另一個論壇上使用malloc的建議,但我甚至不知道它的功能。

在節目本身的一些信息來提供內容:

程序會要求用戶輸入一個名稱(你可以假設 名稱將不超過30個字符)。然後它會找到名稱在1921年到2010年間的 受歡迎程度,並打印出圖表和 圖表。該程序然後會詢問用戶是否希望進行另一個分析並重復該過程。

該方案將在 拉從以下數據源的信息來確定名字的普及。

安大略女寶寶的名字安大略男寶寶名字

注意,一些名字被認爲是男性和女性所以你 程序將不考慮來自這兩個文件需要輸入的名稱 的數據。

我在函數嘗試:

//function that reads and places the read files into the struct arrays 
    int getRawData(FILE* fp, struct nameRecord records[], int currSize) { 
     int i; 
     for(i = 0; i < currSize; i++) { 
      fscanf(fp, "%[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency); 
     } 

而這裏的整個程序:

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

struct nameRecord { 
    char name[31]; 
    int year; 
    int frequency; 
}; 
void allCaps(char[]); 
int getRawData(FILE*, struct nameRecord[], int); 
void setYearTotals(struct nameRecord, int, int); 
void setNameYearTotals(char, struct nameRecord, int, int); 
void getPerHundredThousand(int, int, double); 
void printData(double); 
void graphPerHundredThousand(double); 

int main(void) 
{ 
    int currSizem = 0; 
    int currSizef = 0; 
    struct nameRecord *records; 
    FILE* fp = NULL; 
    FILE* fp2 = NULL; 
    char name[31]; 
    printf("Please enter your name: "); 
    scanf("%30[^\n]", name); 
    printf("your name is %s\n", name); 

//opening both male and female name files and reading them in order to get the total number of records in the array 
    fp = fopen("malebabynames.csv", "r"); 
    if (fp != NULL) { 
     printf("file opened\n"); 
     while(3 == fscanf(fp, "%[^,],%d,%d", records[currSizem].name, &records[currSizem].year, &records[currSizem].frequency)) { 
      currSizem++; 
     } 
    } else { 
     printf("file failed to open\n"); 
    } 
    if(currSizem > 0) { 
     records = malloc(currSizem * sizeof(struct nameRecord)); 
    } 

    fp2 = fopen("femalebabynames.csv", "r"); 
    if (fp != NULL) { 
     printf("file opened\n"); 
     while(3 == fscanf(fp2, "%[^,],%d,%d", records[currSizef].name, &records[currSizef].year, &records[currSizef].frequency)) { 
      currSizef++; 
     } 
    } else { 
     printf("file failed to open\n"); 
    } 
    if(currSizef > 0) { 
     records = malloc(currSizef * sizeof(struct nameRecord)); 
    } 

    return 0; 
} 

//function that automatically capitalizes the users inputted name 
void allCaps(char s[]) { 
    while(*s != '\0') { 
     *s = toupper((unsigned char) *s); 
     s++; 
    } 
} 
//function that reads and places the read files into the struct arrays 
int getRawData(FILE* fp, struct nameRecord records[], int currSize) { 
    int i; 
    for(i = 0; i < currSize; i++) { 
     fscanf(fp, "%[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency); 
    } 
    return 0; 
} 
+0

重複的問題:HTTP://計算器.com/questions/20459531/c-reading-a-text-file-into-a-struct-array –

+1

將文本文件的內容讀入結構似乎不是您的主要問題。如果您的課程未涵蓋動態內存分配,並且分配並未指定文件可能包含的記錄數量,則您必須向教授請求澄清。 –

回答

0

大致如下:

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

struct nameRecord { 
    char name[31]; 
    int year; 
    int frequency; 
}; 

int getRawData(FILE*, struct nameRecord[], int); 

int main(void){ 
    const char *MaleFilePath = "malebabynames.csv"; 
    const char *FemaleFilePath = "femalebabynames.csv"; 
    int currSizem = 0; 
    int currSizef = 0; 
    FILE* mfp = NULL; 
    FILE* ffp = NULL; 
    struct nameRecord *recordsOfMale, *recordsOfFemale; 

//opening both male and female name files and reading them in order to get the total number of records in the array 
    mfp = fopen(MaleFilePath, "r"); 
    if (mfp != NULL) { 
     int dummy; 
     printf("file opened\n"); 
     //line count 
     while(1 == fscanf(mfp, " %*[^,],%*d,%d", &dummy)){ 
      ++currSizem; 
     } 
    } else { 
     printf("file(%s) failed to open\n", MaleFilePath); 
     exit(EXIT_FAILURE); 
    } 
    if(currSizem > 0) { 
     recordsOfMale = malloc(currSizem * sizeof(struct nameRecord)); 
     if(recordsOfMale == NULL){ 
      perror("malloc for Male records"); 
      exit(EXIT_FAILURE); 
     } 
    } 
    rewind(mfp); 
    if(currSizem != getRawData(mfp, recordsOfMale, currSizem)){ 
     fprintf(stderr, "I could not read a record for the specified number\n" 
         "at reading %s\n", MaleFilePath); 
     exit(EXIT_FAILURE); 
    } 
    fclose(mfp); 

    //Do something 

    free(recordsOfMale); 

    return 0; 
} 

//function that reads and places the read files into the struct arrays 
int getRawData(FILE* fp, struct nameRecord records[], int currSize) { 
    int i; 
    for(i = 0; i < currSize; i++) { 
     if(3!=fscanf(fp, " %[^,],%d,%d", records[i].name, &records[i].year, &records[i].frequency)) 
      break; 
    } 
    return i; 
}