将文件指针发送到另一个函数(C编程)

时间:2014-10-03 01:25:51

标签: c function pointers file-io

首先,我承认我是新手(第一年编程,温柔!)。我非常感谢帮助!我一直试图找到这个问题的答案,但是我对指针及其与打开文件的关系的理解有些灰暗。

问题描述:

我希望从文件中读取一堆名称(作为字符数组)并将它们存储在数组中。我之前有过这个工作,但是现在我想用一个名为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函数,它会在调试时向我抛出访问冲突。

1 个答案:

答案 0 :(得分: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. 正如评论中所提到的,如果您对成功打开文件的检查失败,则不要退出。

  5. 更新的代码:

    #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
    

    给出输出:

    paul@local:~/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
    paul@local:~/Documents/src/sandbox$