2014-10-03 83 views
2

首先,我承認我是新手(第一年編程,要溫柔!)。我會非常感謝幫助!我一直在試圖找到這個問題的答案,但是我對指針的理解以及它們與打開文件的關係有些灰暗。問題的發送文件指針到另一個函數(C編程)

說明:

我想從文件中讀取一串的名字(如字符數組),並將它們存儲在數組中。我之前有過這樣的工作,但現在我想在名爲get_names()的不同函數中填充名稱,同時處理main()中打開的文件。

我迄今爲止代碼:

#include "stdafx.h" 
    #include <string.h> 
    #include <math.h> 

    typedef struct {   // Custom data type for holding player information 
    char name[30];  // Name of Player 
    int  overs,   // No. of overs 
      maidens,  // No. of Maidens 
      runs,   // No. of runs scored 
      wickets;  // No. of wickets scored 
    } member_t; 

    /* Function Prototypes */ 
    void get_names(member_t allplayers[], FILE *input); 

    /* Function: To get names from file and place them in allplayers[] array */ 
    void get_names(member_t allplayers[], FILE *input) 
    { 
     member_t current_player; // Current player being read from file 
     int  input_status; // Status value returned by fscanf 
     int  i = 0; 

    input_status = fscanf(input, "%s", &current_player.name); /////ISSUE HERE?? 

    while (input_status != -1) 
    { 
     strcpy(allplayers[i].name, current_player.name); 
     allplayers[i].overs = 0; 
     allplayers[i].maidens = 0; 
     allplayers[i].runs = 0; 
     allplayers[i].wickets = 0; 

     input_status = fscanf(input, "%s", &current_player.name); 
     i += 1; 
    } 
    } 

/* Main Function */ 
int 
main(void) 
{ 
    FILE *fileinput_a; // Pointer to file input2a.dat for names 
    int  i; 

    member_t allplayers[15]; 

    fileinput_a = fopen("F:\\input2a.dat", "r"); // Opens file for reading 
    if (!fileinput_a)        // Checks to see if file has any data 
     printf("File open error - File is empty"); // Empty file error 

    get_names(&allplayers[15], fileinput_a);  // Send array as an output, and file pointer as input 

    for (i = 0; i < 15; i++) 
    { 
     printf("%10s ", allplayers[i].name); 
     printf("%d ", allplayers[i].overs); 
     printf("%d ", allplayers[i].maidens); 
     printf("%d ", allplayers[i].runs); 
     printf("%d\n", allplayers[i].wickets); 
    } 

    fclose(fileinput_a); 

    return(0); 
} 

的Visual Studio 2013似乎並沒有與代碼中的問題,但是一旦進入到標記fscanf函數,它拋出一個訪問衝突在我的時候調試。

+2

get_names(allplayers [15],fileinput_a) ;你想在這裏做什麼,因爲它確實看起來不正確。 – OldProgrammer 2014-10-03 01:30:31

+0

還在縮進! 此外,我認爲大括號是get_names()的近括號。前一個是關閉我的while循環。當我從我的程序中複製它時,它一定會被移動。 – Yoshi 2014-10-03 01:30:37

+1

您測試文件指針,但您的打印語句不會以換行符結束,因此您可能看不到該消息,並且您肯定繼續,因爲它一切正常。它可能不是好的。在printf(「文件打開錯誤... \ n」);'語句之後添加一個'return(1);'後面(並且用大括號括起來)。 – 2014-10-03 01:31:50

回答

1

你有許多問題:

  1. input_status = fscanf(input, "%s", &current_player.name);應該是在這兩個地方input_status = fscanf(input, "%s", current_player.name);current_player.name已衰減到指針。

  2. get_names(&allplayers[15], fileinput_a);應該是get_names(allplayers, fileinput_a);,因爲傳遞一個元素的地址超過數組的末尾是沒有意義的。

  3. while (input_status != -1)應該while (input_status && input_status != EOF)因爲fscanf()也可以合法地返回零,而不是EOF,你不應該在EOF等於-1計數。

  4. 正如在評論中提到的,如果你檢查成功的文件打開失敗,你不應該退出,你應該這樣做。

更新代碼:

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

typedef struct { 
    char name[30]; 
    int overs; 
    int maidens; 
    int runs; 
    int wickets; 
} member_t; 

/* Function: To get names from file and place them in allplayers[] array */ 
void get_names(member_t allplayers[], FILE * input) { 
    member_t current_player; 
    int input_status; 
    int i = 0; 

    input_status = fscanf(input, "%s", current_player.name); 

    while (input_status && input_status != EOF) { 
     strcpy(allplayers[i].name, current_player.name); 
     allplayers[i].overs = 0; 
     allplayers[i].maidens = 0; 
     allplayers[i].runs = 0; 
     allplayers[i].wickets = 0; 

     input_status = fscanf(input, "%s", current_player.name); 
     i += 1; 
    } 
} 

/* Main Function */ 
int main(void) { 
    FILE *fileinput_a; 
    int i; 
    member_t allplayers[15]; 

    fileinput_a = fopen("crickdat", "r"); 
    if (!fileinput_a) { 
     printf("File open error - File is empty"); 
     return EXIT_FAILURE; 
    } 

    get_names(allplayers, fileinput_a); 

    for (i = 0; i < 15; i++) { 
     printf("%10s ", allplayers[i].name); 
     printf("%d ", allplayers[i].overs); 
     printf("%d ", allplayers[i].maidens); 
     printf("%d ", allplayers[i].runs); 
     printf("%d\n", allplayers[i].wickets); 
    } 

    fclose(fileinput_a); 
    return 0; 
} 

當在文件crickdat運行:

walker 
simpson 
derrick 
parker 
phillips 
dawson 
fiddler 
wharton 
inglis 
farquah 
finnegan 
dobson 
wrangler 
gaston 
brankle 

給出的輸出:

[email protected]:~/Documents/src/sandbox$ ./cricket 
    walker 0 0 0 0 
    simpson 0 0 0 0 
    derrick 0 0 0 0 
    parker 0 0 0 0 
    phillips 0 0 0 0 
    dawson 0 0 0 0 
    fiddler 0 0 0 0 
    wharton 0 0 0 0 
    inglis 0 0 0 0 
    farquah 0 0 0 0 
    finnegan 0 0 0 0 
    dobson 0 0 0 0 
    wrangler 0 0 0 0 
    gaston 0 0 0 0 
    brankle 0 0 0 0 
[email protected]:~/Documents/src/sandbox$ 
+0

保羅 - 謝謝!它現在似乎有魅力。我正在通過代碼來找到我犯的錯誤......我發誓我已經提出了建議的更改,但仍然無法正常工作,而這一次似乎是這樣。 – Yoshi 2014-10-03 01:51:23