如何将文本文件中的值分配给C中的数组结构?

我必须在C做选举。

共有7名候选人和365票。 我需要使用一组结构来做到这一点。 我需要从文本文件中读取候选人的每个名字和他们得到的票数。 最后我需要输出选举的胜利者。

以下是我的代码示例

#include  #include  #include  struct candidates { char name[20]; int votes; }; int main() { //Counter int i = 0; int gVotes = 0; //Votes counter int v = 0; //Sploit Vote int spVote = 0; struct candidates electionCandidate[7]; FILE *fp; fp = fopen("elections.txt", "r"); for(i=0;i<7;i++) { char * aNames = fgets(electionCandidate[i].name, 20, fp); } //for testing each candidate gots their name for(i=0;i<7;i++) { printf("%d. Candidate is %s\n\n", i+1, electionCandidate[i]); } //For 365 Votes while (!feof(fp)) { int iVoteFor = 0; fscanf(fp, "%d", &iVoteFor); electionCandidate[iVoteFor-1].votes++; //gVotes is my counter for the number of entries. printf("%d ", ++gVotes); } system("pause"); return 0; } //Ideas of what to use 

这是我目前的elections.txt

 Robert Bloom John Brown Michelle Dawn Michael Hall Sean O'Rielly Arthur Smith Carl White 3 3 81 1 2 3 1 2 4 5 1 6 12 9 6 5 0 2 8 46 6 8 3 2 8 0 12 6 1 8 3 11 7 5 5 8 9 10 12 1 3 12 2 23 2 5 7 4 11 8 6 11 12 9 11 7 9 3 1 2 10 12 12 7 11 9 6 6 0 1 10 7 11 2 8 0 12 8 10 11 2 2 8 4 2 12 3 2 9 1 4 88 7 7 4 12 2 10 10 9 4 12 9 3 12 0 48 0 6 5 9 0 5 3 11 6 0 3 0 1 2 3 4 1 1 2 3 3 3 3 3 3 3 3 3 3 8 4 5 9 1 2 12 1 7 7 7 7 7 7 7 7 7 7 7 4 7 1 2 4 5 1 2 3 1 2 8 7 12 95 41 1 7 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 6 6 6 6 6 7 7 7 7 7 8 8 9 9 8 7 7 1 1 2 3 5 4 4 6 8 7 52 1 4 7 5 2 5 4 7 7 7 7 7 7 7 4 7 7 7 1 2 5 4 7 8 7 4 1 4 7 8 7 4 1 5 2 5 2 3 6 5 3 2 1 2 1 2 3 2 2 5 1 4 7 7 7 7 7 7 7 7 7 7 7 7 1 2 1 3 4 5 1 2 3 1 2 3 1 4 5 8 1 2 4 1 4 2 5 6 7 8 1 2 3 3 4 7 7 7 7 7 7 7 8 1 2 3 4 

编辑:

每个候选人获得+1票数选举候选人[0],每个候选人获得一票,等等。 共有365名选民。

我能够从文本文件中输入每个候选人的名字。 现在问题是每个投票给相应的候选人。 此外,任何高于7的投票都是一个被宠坏的投票,我试图在上面的代码中计算。 代码编译但崩溃了。

我正在使用while(!feof),但似乎它不起作用或这不是正确的方法。 有什么建议。

编辑:我正在使用调试器,并发现它运行了几次(!feof),但在其中一个实例中,它给出了错误并停止。

编辑:通过评论行选举候选[iVoteFor-1] .votes ++; 程序一直读到365值。

如何指定每个候选人投票?

 #include  #include  #include  struct candidate { char name[20]; int votes; }; enum { MAX_CANDIDATES = 7 }; static int vote_cmp(const void *vp1, const void *vp2) { int votes1 = ((const struct candidate *)vp1)->votes; int votes2 = ((const struct candidate *)vp2)->votes; if (votes1 < votes2) return -1; else if (votes1 > votes2) return +1; else return 0; } int main(void) { struct candidate electionCandidate[MAX_CANDIDATES]; char buffer[4096]; size_t i; for (i = 0; fgets(buffer, sizeof(buffer), stdin) != 0; i++) { if (strlen(buffer) < 2) break; // Blank line if (i >= MAX_CANDIDATES) { fprintf(stderr, "Too many candidates: %s", buffer); return 1; } size_t len = strlen(buffer); if (len >= sizeof(electionCandidate[i].name)) { fprintf(stderr, "Candidate name too long: %s", buffer); return 1; } memmove(electionCandidate[i].name, buffer, len - 1); electionCandidate[i].name[len] = '\0'; electionCandidate[i].votes = 0; } size_t n_cand = i; int spoiled = 0; unsigned votefor; while (scanf("%u", &votefor) == 1) { if (votefor == 0 || votefor > n_cand) spoiled++; else electionCandidate[votefor-1].votes++; } qsort(electionCandidate, n_cand, sizeof(electionCandidate[0]), vote_cmp); for (i = 0; i < n_cand; i++) printf("%20s: %3d\n", electionCandidate[i].name, electionCandidate[i].votes); putchar('\n'); printf("%20s: %3d\n", "Spoiled votes", spoiled); return 0; } 

给定样本数据,将O'Rielly先生的名称从UTF-8修改为ASCII,输出为:

  Arthur Smith: 17 Sean O'Rielly: 21 Michelle Dawn: 33 John Brown: 36 Robert Bloom: 39 Michael Hall: 40 Carl White: 64 Spoiled votes: 77 

显然,“被玷污的选票”是正式当选的赢家。

为了从文件中读取字符串,您需要使用fgets而不是fscanf,因为fgets读取整个字符串。 因此,读取字符串应如下所示:

 int main() { int i = 0; // initialize everything to zeroes // otherwise they should be initialized manually in a loop struct candidates electionCandidate[7] = {0}; FILE *file = fopen("elections.txt", "r"); for (i = 0; i<7; i++) { char * res = fgets(electionCandidate[i].name, 100, file); printf("%s\n", res); } //strcpy(electionCandidate[0].name, "Daniel Guzman"); //electionCandidate[0].votes = 41; //printf("%s got %d Votes\n\n", electionCandidate[0].name, electionCandidate[0].votes); system("pause"); return 0; } 

阅读投票和计算将如下所示:

 while (!feof(file)) { int iVoteFor = 0; fscanf(file, "%d", &iVoteFor); electionCandidate[iVoteFor-1].votes++; } 

我相信以后很容易找到胜利者。