我收到一个名为 circle.txt
的 txt 文件。该 txt 文件由姓名和 Jersey 号码组成,例如
Cameron 1
David 2
Dorothy 3
Heather 4
等...到 100
所以我必须做一个基于游戏计数到驱逐的项目,该项目的一部分需要创建一个循环双链表
使用 void createGame(char* gameFile, int numOfPlayers);
函数指定指定数量的玩家,以便每个链接代表一个玩家。然后我必须根据从文件中读取的值设置球员的姓名和 Jersey 号码。所以我的头文件包含所有功能,看起来像
countOust.h
#ifndef CountOust_h
#define CountOust_h
struct listNode
{
int hName; //represents numbers in file
char data[15]; // represents name in file
struct listNode *next;
struct listNode *prev;
};
typedef struct listNode sNode;
void createGame(char *gameFile, int numOfPlayers);
//void traverseFwd(sNode *list);
//void insertAt(sNode *list, sNode *player);
//void lRemove(sNode *player);
//void traverseBwd(sNode *list);
//void startGame();
#endif
一些函数被注释掉,因为主要焦点是 createGame
函数。
因此,我尝试使用 fopen
和使用 fscanf
的 for 循环填充 countOust.c
列表中的链接。我尝试过类似的事情
countOust.c
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <stdbool.h>
#include "CountOust.h"
void createGame(char *gameFile, int numOfPlayers)
{
FILE* f;
f = fopen(gameFile, "r");
// head = (struct sNode *)malloc(sizeof(sNode);
fscanf(f, "%s %d", head->data, &head->hName);
for (int i = 0; i < numOfPlayers - 1; i++)
{
}
fscanf(f, "%s %d", tail->data, &tail->hName);
}
^这会产生错误,因为它的头脑 Storm 代码
我需要一个 fscanf,它将包含一个字符串和整数,表示文件中的名称和文件中的数字。
我需要设置 为节点分配空间,并且该节点应该具有对下一个节点的名称编号引用和对上一个节点的引用。然后我需要设置名称和编号,然后设置上一个和下一个节点的指针。
那么我将如何创建一个循环双链表并使用 fscanf、malloc 填充球员姓名和 Jersey 号码的 txt 文件,引用头文件结构中的节点,以从 txt 文件创建循环双链表?
最佳答案
假设 gamefile
包含您的 circle.txt
文件的名称,您的 fscanf()
调用将仅读取当前写入的数字。您还需要添加一些内容来读取名称。
fscanf(f, "%s %d", args...)
将是一个很好的起点。
关于c - 用txt文件填充循环双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846485/