我正在尝试将外部FILE中的列表上传到链接列表中,并且可以在列表中访问FILE中的所有字符。到目前为止我有这个,但它只包含输入文件列表中的最后一个单词。我不知道问题是什么,我不知道从哪里开始。任何帮助都会很棒!谢谢。
我上传的.txt文件只是一堆无意义的文字,如:
Ted
Greg
Shoe
Money
Apple
当我运行程序时,列表只包含Apple。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 15
#define INPUT 1
#define OUTPUT 2
struct tree_node{
char string[MAXLEN+1];
struct tree_node *left_child, *right_child;
}*first = NULL;
int main(int argc, char *argv[])
{
char cell[MAXLEN];
int op;
FILE *pf;
FILE *out;
pf = fopen(("%s", argv[INPUT]), "r");
if(pf == NULL){
fprintf(stderr, "Error: File is Empty. \n");
return 0;
}else{
struct tree_node *temp;
struct tree_node *nn=(struct tree_node*)malloc(sizeof(struct tree_node));
while(!feof(pf)){
// fgets(&nn->string, MAXLEN, pf);
fscanf(pf, "%s", &nn->string); //I think this is where the problem is.
if(first != NULL){
temp = first;
while(temp -> right_child != NULL)
temp = temp -> right_child;
temp -> right_child = nn;
}else{
first = nn;
}
nn->right_child = NULL;
}
}
do{
printf("1.Display.\n2.Exit.\n");
printf("Selection?\n");
scanf("%d", &op);
switch(op)
{
case 1:display();
break;
}
}
while(op < 2 && op > 0);
}
int display()
{
struct tree_node *temp;
temp = first;
if(temp == NULL)
{
printf("EMPTY!\n");
return;
}
printf("Elements: \n");
while(temp != NULL){
printf("%s\n", temp -> string);
temp = temp -> right_child;
}
}
答案 0 :(得分:2)
fscanf(pf, "%s", &nn->string);
这里string
是char数组。所以删除&
。
您只创建了一个节点。以上部分为()。
struct tree_node *temp;
struct tree_node *nn=(struct tree_node*)malloc(sizeof(struct tree_node));
需要为每个字符串创建单独的节点。
答案 1 :(得分:0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 15
#define INPUT 1
#define OUTPUT 2
#define EXIT 2
#define _S(x) #x
#define S(x) _S(x)
struct tree_node{
char string[MAXLEN+1];
struct tree_node *left_child, *right_child;
}*first = NULL;
void display(void);
void add_tree(char string[MAXLEN+1]);
int main(int argc, char *argv[]){
char cell[MAXLEN+1];
int op;
FILE *pf, *out;
if(NULL==(pf = fopen(argv[INPUT], "r"))){
fprintf(stderr, "Error: File can't open.\n");
return 1;
}
while(fscanf(pf, " %" S(MAXLEN) "[^\n]", cell)==1){
add_tree(cell);
}
fclose(pf);
do{
printf("1.Display.\n2.Exit.\n");
printf("Selection?\n");
scanf("%d", &op);
switch(op){
case 1:
display();
break;
}
} while(op != EXIT);
//release tree
return 0;
}
struct tree_node* new_node(char string[MAXLEN+1]){
struct tree_node *np = malloc(sizeof(*np));
if(np){
strcpy(np->string, string);
np->left_child = np->right_child = NULL;
}
return np;
}
void insert(struct tree_node **np, char string[MAXLEN+1]){
int cmp;
if(*np == NULL){
*np = new_node(string);
return;
}
if(0==(cmp=strcmp((*np)->string, string)))
return;
if(cmp > 0)
insert(&(*np)->left_child, string);
else
insert(&(*np)->right_child, string);
}
void add_tree(char string[MAXLEN+1]){
insert(&first, string);
}
void display_r(struct tree_node *np){
if(np){
display_r(np->left_child);
printf("%s\n", np->string);
display_r(np->right_child);
}
}
void display(void){
if(first == NULL){
printf("EMPTY!\n");
return;
}
printf("Elements: \n");
display_r(first);
}