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

标签 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. input_status = fscanf(input, "%s", ¤t_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

给出输出:

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$ 

关于c - 将文件指针发送到另一个函数(C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172043/

相关文章:

C 预处理器 : line continuation: why exactly comment is not allowed after backslash character ('\' )?

c - 这个 C qsort 函数调用中的参数是什么?

c++ - 指向 char 数组的指针,未处理的异常

c - Linux 中的当前语言环境如何影响在 C 代码中使用文件名?

c++ - 等待 recv 中的数据时关闭线程

c++ - visual studio 2015 中的安全功能

javascript - 如何在 JavaScript 中只触发一次函数

c - 变量返回还是直接返回?

c - 选择排序递归函数的段错误

c - 从队列中使用 dequeue 方法时抛出异常