从函数更改结构数组(从文件读取)

时间:2014-09-04 18:51:31

标签: c

我有以下结构

struct candidates
    {
     char name[20];
     int votes;
    };
struct candidates electionCandidates[];

我需要从文件读入并更新名称electionCandidates [0]到7。

我可以使用以下

这样做
for (i = 0; i < 7; i++)
    {
        fgets(electionCandidates[i].name, 20, (FILE*)fp);
    }

但我需要在一个函数中执行此操作。

我试过了

void Initialize(struct candidates* EC[]);

Initialize(&electionCandidates);

void Initialize(struct candidates* EC[])
{
    int i;

    FILE *fp;
    fp = fopen("elections.txt", "r");

    for (i = 0; i < 7; i++)
    {
        fgets(EC[i].name, 20, (FILE*)fp);
    }

    fclose(fp);
}

坚持说它没有名字。或者整件事可能是错的。我不确定。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

您需要查看您的Initialize()函数签名,并打开编译器警告(如果它们已经打开,请注意它们)。您声明Initialize()获取指向struct candidates的指针数组,这不是您要传递的数组struct candidates。数组衰减为指针,因此struct candidates *EC是你的参数应该是什么样的(或者,struct candidates EC[],在这种情况下是等价的),而不是struct candidates *EC[]。然后在没有& ...

的情况下调用您的函数

答案 1 :(得分:0)

鉴于

struct candidates
    {
     char name[20];
     int votes;
    };
struct candidates electionCandidates[];

你想要

void Initialize(struct candidates EC[])
{
    /* ... */
}

Initialize(electionCandidates);

当作为函数参数传递时,结构数组会衰减为指向其第一个元素的指针。

答案 2 :(得分:0)

我得到它加载名称和一切,但我想我需要指向SPOIL在这?它不是在阅读或做选票/被破坏的选票,两者都是0。

#include <stdio.h>

struct candidates
{
 char name[20];
 int votes;
};
struct candidates electionCandidates[7];

void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int BadVote);
void printResults(struct candidates EC[], int BadVote);

int main()

{

    int i, SPOIL = 0;

    Initialize(electionCandidates);
    Processvotes(electionCandidates, SPOIL);
    printResults(electionCandidates, SPOIL);

}

void Initialize(struct candidates EC[])
{
    int i;

    FILE *fp;
    fp = fopen("elections.txt", "r");

    for (i = 0; i < 7; i++)

    {
        fgets(EC[i].name, 20, (FILE*)fp);
    }

    fclose(fp);
}


void Processvotes(struct candidates EC[], int BadVote)
{
        int TVOTE, i;

        FILE *fp;
        fp = fopen("elections.txt", "r");

        fscanf(fp, "%d", &TVOTE);

        for (i = 0; i < 365; i++)
        {
            if (TVOTE == 1)
                EC[0].votes++;
            if (TVOTE == 2)
                EC[1].votes++;
            if (TVOTE == 3)
                EC[2].votes++;
            if (TVOTE == 4)
                EC[3].votes++;
            if (TVOTE == 5)
                EC[4].votes++;
            if (TVOTE == 6)
                EC[5].votes++;
            if (TVOTE == 7)
                EC[6].votes++;

            if (TVOTE < 1 || TVOTE > 7)
                BadVote++;
        }

        fclose(fp);
}

void printResults(struct candidates EC[], int BadVote)
{

    int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;

    for (i = 0; i < 7; i++)
    {

        if (EC[i].votes > WinSCORE)
        {
            WinSCORE = EC[i].votes;
            Win = i;
        }

        if (EC[i].votes == WinSCORE)
        {
            RunnerSCORE = EC[i].votes;
            Runner = i;
        }
    }


    if (WinSCORE == RunnerSCORE)
    {
        printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, BadVote);
    }

    else
        printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, BadVote);


}