文字列の読み込みができないのはなぜでしょうか.
ファイルオープンのエラーはありません.
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct BTREE_ {
struct BTREE_ *left, *right;
char data[20];
}Btree;
Btree *newCell(char *data) {
Btree *bp;
bp = (Btree *)malloc(sizeof(Btree));
bp->left = NULL;
bp->right = NULL;
strcpy(bp->data, data);
return bp;
}
Btree *registBtree(Btree *node, char *newdata) {
int len1, len2;
len1 = strlen(node->data);
len2 = strlen(newdata);
if (node == NULL) {
node = newCell(newdata);
}
else if (len2 < len1) {
node->left = registBtree(node->left, newdata);
}
else {
node->right = registBtree(node->right, newdata);
}
return node;
}
/* 部分木の全ての値を出力 */
Btree *printBtree(Btree *node) {
if (node == NULL) {
return NULL;
}
printBtree(node->left);
printf("data[%s]\n", node->data);
printBtree(node->right);
return node;
}
int main() {
FILE*fp;
Btree *start;
//int newdata = 0;
char newdata[20];
if ((fp = fopen("food.txt", "r")) == NULL) {
fprintf(stderr, "%s\n", "error: can't read file.");
return EXIT_FAILURE;
}
fscanf(fp, "%s", newdata);//root
start = newCell(newdata);
while (fscanf(fp, "%s", newdata) != EOF) {
registBtree(start, newdata);
}
printBtree(start);
return 0;
}