2014-10-03 82 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