如何从文件填充链接列表
How to populate a linked list from a file
我正在尝试填充从文件读取的链接列表。我一直在尝试使用 strtok()
将 " "
用作分隔符的字符串分开。当我调用我的 addEntry()
函数时,它只是将每个字符串的第一个字母添加到一个新节点。我意识到这可能是一个简单的疏忽,所以请原谅我的无知,因为我对编码还很陌生。
我遇到问题的具体代码部分是:
void readData(struct boggleDataNode **headRef){
//initializing variables including
//opening the input file
int counter = 96;
int i = 0;
char dataLine[LENGTH];
char *data;
char *fileName = "BoggleData.txt";
FILE *filePointer;
printf("Trying to open file %s\n", fileName);
filePointer = fopen(fileName, "r"); //read mode
printf("Successfully opened file %s\n", fileName);
if (filePointer == NULL){
perror("Error while opening file. \n");
exit(0);
}
//loop until find end of file
while ( fgets(dataLine, LENGTH, filePointer) != NULL){
//I feel like i should be using a nested loop here but I can't figure out how to do it
printf("dataLine = %s\n", dataLine);
data = strtok(dataLine, " ");
printf("data = %s\n", data);
//fgets(data, 3, filePointer);
addEntry(headRef, data);
//nested loop should end here
}
}
我的输出只有16个节点,每行的第一个字母,当它应该是96个节点长的时候。
我正在阅读的文件是:
D R L X E I
C P O H S A
N H N L Z R
W T O O T A
I O S S E T
N W E G H E
B O O J A B
U I E N E S
P S A F K F
I U N H M Qu
Y R D V E L
V E H W H R
I O T M U C
T Y E L T R
S T I T Y D
A G A E E N
如果需要完整代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LENGTH 80
struct boggleDataNode {
char data[3];
struct boggleDataNode *nextData;
};
struct boggleDieSideNode {
char dieSideData[3];
struct boggleDieSideNode *nextSide;
};
void readData(struct boggleDataNode **);
void addEntry(struct boggleDataNode **, char *);
void display(struct boggleDataNode *);
int main() {
int counter = 0;
struct boggleDataNode * head = NULL;
struct boggleDieSideNode *head1 = NULL;
readData(&head);
display(head);
system("pause");
return 0;
}
void readData(struct boggleDataNode **headRef){
//initializing variables including
//opening the input file
int counter = 96;
int i = 0;
char dataLine[LENGTH];
char *data;
char *fileName = "BoggleData.txt";
FILE *filePointer;
printf("Trying to open file %s\n", fileName);
filePointer = fopen(fileName, "r"); //read mode
printf("Successfully opened file %s\n", fileName);
if (filePointer == NULL){
perror("Error while opening file. \n");
exit(0);
}
//loop until find end of file
while ( fgets(dataLine, LENGTH, filePointer) != NULL){
printf("dataLine = %s\n", dataLine);
data = strtok(dataLine, " ");
printf("data = %s\n", data);
//fgets(data, 3, filePointer);
addEntry(headRef, data);
}
fclose(filePointer);
}//cose readData
void addEntry(struct boggleDataNode **headRef, char *data){
//allocate node
struct boggleDataNode* temp = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
//put in data
strcpy(temp->data, data);
// link the old list off the new node
temp->nextData = (*headRef);
// move the head to point to the new node
(*headRef) = temp;
}
void display(struct boggleDataNode *headRef){
struct boggleDataNode *helper = headRef;
if (helper == NULL){
return;
}
else{
printf("============================================\n");
printf("Node # Data\n");
printf("============================================\n");
int counter = 1;
while (helper != NULL){
printf("%5d \t %2s\n", counter++, helper->data);
helper = helper->nextData;
}
}
printf("\n");
system("pause");
}
你说得对,你需要一个嵌套循环。
strtok
需要这样调用:
for (char *tok = strtok("input", " "); tok; tok = strtok(NULL, " "))
{
/* use tok */;
}
- 如果您只对单个字符感兴趣,则无需阅读整行(并将它们切成单个字符)
- 由于您只对单个字符感兴趣,因此结构中的
.data
元素不需要 char data[3]
- 读取文件的函数可以将文件名作为参数
- 并且该函数可以 return 它已读取的列表,而不是 return 无效并将结果通过指向指针的指针传递回调用者。
#include <stdio.h>
#include <stdlib.h>
struct boggleNode {
struct boggleNode *next;
char data;
};
struct boggleNode * readboggle(char *name);
int main (int argc, char **argv)
{
struct boggleNode *bp;
if (!argv[1]) return 0;
bp = readboggle( argv[1] );
for ( ; bp; bp = bp-> next) {
printf(" %c", bp->data);
}
printf("\n" );
return 0;
}
struct boggleNode * readboggle(char *name)
{
struct boggleNode *ret=NULL, **bpp;
FILE *fp;
int ch;
fp = fopen (name, "r" );
if (!fp) return NULL;
ret= NULL;
bpp = &ret;
while(1) {
ch = getc(fp);
if (ch == EOF) break;
if (ch < 'A' || ch > 'Z') continue;
*bpp = malloc (sizeof **bpp);
(*bpp)->data = ch;
(*bpp)->next = NULL;
bpp = &(*bpp)->next;
}
fclose (fp);
return ret;
}
我正在尝试填充从文件读取的链接列表。我一直在尝试使用 strtok()
将 " "
用作分隔符的字符串分开。当我调用我的 addEntry()
函数时,它只是将每个字符串的第一个字母添加到一个新节点。我意识到这可能是一个简单的疏忽,所以请原谅我的无知,因为我对编码还很陌生。
我遇到问题的具体代码部分是:
void readData(struct boggleDataNode **headRef){
//initializing variables including
//opening the input file
int counter = 96;
int i = 0;
char dataLine[LENGTH];
char *data;
char *fileName = "BoggleData.txt";
FILE *filePointer;
printf("Trying to open file %s\n", fileName);
filePointer = fopen(fileName, "r"); //read mode
printf("Successfully opened file %s\n", fileName);
if (filePointer == NULL){
perror("Error while opening file. \n");
exit(0);
}
//loop until find end of file
while ( fgets(dataLine, LENGTH, filePointer) != NULL){
//I feel like i should be using a nested loop here but I can't figure out how to do it
printf("dataLine = %s\n", dataLine);
data = strtok(dataLine, " ");
printf("data = %s\n", data);
//fgets(data, 3, filePointer);
addEntry(headRef, data);
//nested loop should end here
}
}
我的输出只有16个节点,每行的第一个字母,当它应该是96个节点长的时候。 我正在阅读的文件是:
D R L X E I
C P O H S A
N H N L Z R
W T O O T A
I O S S E T
N W E G H E
B O O J A B
U I E N E S
P S A F K F
I U N H M Qu
Y R D V E L
V E H W H R
I O T M U C
T Y E L T R
S T I T Y D
A G A E E N
如果需要完整代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LENGTH 80
struct boggleDataNode {
char data[3];
struct boggleDataNode *nextData;
};
struct boggleDieSideNode {
char dieSideData[3];
struct boggleDieSideNode *nextSide;
};
void readData(struct boggleDataNode **);
void addEntry(struct boggleDataNode **, char *);
void display(struct boggleDataNode *);
int main() {
int counter = 0;
struct boggleDataNode * head = NULL;
struct boggleDieSideNode *head1 = NULL;
readData(&head);
display(head);
system("pause");
return 0;
}
void readData(struct boggleDataNode **headRef){
//initializing variables including
//opening the input file
int counter = 96;
int i = 0;
char dataLine[LENGTH];
char *data;
char *fileName = "BoggleData.txt";
FILE *filePointer;
printf("Trying to open file %s\n", fileName);
filePointer = fopen(fileName, "r"); //read mode
printf("Successfully opened file %s\n", fileName);
if (filePointer == NULL){
perror("Error while opening file. \n");
exit(0);
}
//loop until find end of file
while ( fgets(dataLine, LENGTH, filePointer) != NULL){
printf("dataLine = %s\n", dataLine);
data = strtok(dataLine, " ");
printf("data = %s\n", data);
//fgets(data, 3, filePointer);
addEntry(headRef, data);
}
fclose(filePointer);
}//cose readData
void addEntry(struct boggleDataNode **headRef, char *data){
//allocate node
struct boggleDataNode* temp = (struct boggleDataNode*) malloc(sizeof(struct boggleDataNode));
//put in data
strcpy(temp->data, data);
// link the old list off the new node
temp->nextData = (*headRef);
// move the head to point to the new node
(*headRef) = temp;
}
void display(struct boggleDataNode *headRef){
struct boggleDataNode *helper = headRef;
if (helper == NULL){
return;
}
else{
printf("============================================\n");
printf("Node # Data\n");
printf("============================================\n");
int counter = 1;
while (helper != NULL){
printf("%5d \t %2s\n", counter++, helper->data);
helper = helper->nextData;
}
}
printf("\n");
system("pause");
}
你说得对,你需要一个嵌套循环。
strtok
需要这样调用:
for (char *tok = strtok("input", " "); tok; tok = strtok(NULL, " "))
{
/* use tok */;
}
- 如果您只对单个字符感兴趣,则无需阅读整行(并将它们切成单个字符)
- 由于您只对单个字符感兴趣,因此结构中的
.data
元素不需要char data[3]
- 读取文件的函数可以将文件名作为参数
- 并且该函数可以 return 它已读取的列表,而不是 return 无效并将结果通过指向指针的指针传递回调用者。
#include <stdio.h>
#include <stdlib.h>
struct boggleNode {
struct boggleNode *next;
char data;
};
struct boggleNode * readboggle(char *name);
int main (int argc, char **argv)
{
struct boggleNode *bp;
if (!argv[1]) return 0;
bp = readboggle( argv[1] );
for ( ; bp; bp = bp-> next) {
printf(" %c", bp->data);
}
printf("\n" );
return 0;
}
struct boggleNode * readboggle(char *name)
{
struct boggleNode *ret=NULL, **bpp;
FILE *fp;
int ch;
fp = fopen (name, "r" );
if (!fp) return NULL;
ret= NULL;
bpp = &ret;
while(1) {
ch = getc(fp);
if (ch == EOF) break;
if (ch < 'A' || ch > 'Z') continue;
*bpp = malloc (sizeof **bpp);
(*bpp)->data = ch;
(*bpp)->next = NULL;
bpp = &(*bpp)->next;
}
fclose (fp);
return ret;
}