将数据文件读入二叉树

时间:2013-08-06 05:20:03

标签: structure binary-search-tree readfile

我的程序似乎只读取部分输入数据文件

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#include "BST_ADT.h"

// Structure
typedef struct
    {
        char* name;
        char* market;
        char* initial;
        float stock;
    }COMPANY;

// Prototype Delarations
void addComp    (BST_TREE* list);
void deleteComp (BST_TREE* list);
void findStu   (BST_TREE* list);
void printList (BST_TREE* list);
int  compareStu (void* stu1, void* stu2);
void processStu (void* dataPtr);


int main (void)
{
// Local Definitions
   BST_TREE* list;

// Statements
   list = BST_Create(compareStu);
   addComp(list);
   deleteComp(list);
   findStu (list);
   printList(list);

    return 0;
}


/*===================== addComp =========================*/

void addComp    (BST_TREE* list)
{
// Local Declarations
   COMPANY* stuPtr;
   FILE* fp;
   char fileName[25];
   char buffer [100];
// Statements

   stuPtr = (COMPANY*)malloc (sizeof (COMPANY));
   stuPtr->name = (char*) malloc(128 * sizeof(char));
   stuPtr->market = (char*) malloc(128 * sizeof(char));
   stuPtr->initial = (char*) malloc(128 * sizeof(char));

   printf("Enter the file name: ");
           gets(fileName);
           fp = fopen(fileName, "r");
           if(fp == NULL)
           {
              printf("Error cannot open the file!\n");
              exit(101);
           }
   while(fgets(buffer, 100, fp) != NULL)
        {
            if (!stuPtr)
                printf("MEmory overflow!\n"), exit(101);
            sscanf(buffer, "%s  %s  %s  %f ", stuPtr->name, stuPtr->market, stuPtr->initial, &(stuPtr->stock));
            BST_Insert(list, stuPtr);
        } // end while
} //addStu

/*===================== deleteComp =========================*/

void deleteComp (BST_TREE* list)
{
// local definitions
   char name[100];
   char* namePtr = (char*) malloc(128 * sizeof(char));
   namePtr = name;
// statements
   printf("Enter Company name: ");
   scanf ("%39s", namePtr);

   if (!BST_Delete (list, namePtr))
       printf("ERROR: No Company: %0\n", *namePtr);
} // deleteStu

这是我从文本文件输入的数据:

Target Corporation;      NYSE   TGT     44.14B  
PriceSmart, Inc.;        NASDAQ PSMT    721.96M   
Eastman Kodak Company;   NYSE   EK      5.14B  
Concord Camera Corp.;    NASDAQ LENS    25.43M  
Siemens AG (ADR);        NYSE   SI      123.13B  
3M Company;              NYSE   MMM     56.47B  
Toshiba Corporation;     OTC    TOSBF   24.43B  
Tyco International Ltd.; NYSE   TYC     19.91B  
Textron Inc.;            NYSE   TXT     13.90B  
PHH Corporation;         NYSE   PHH     1.11B  
Activision, Inc.;        NASDAQ ATVI    8.07B 
The Walt Disney Company; NYSE   DIS     61.27B 

在addComp函数的末尾

name contain 'The'
market contain 'Walt
initial contain 'Disney
and stock contain 6.09

这不是该功能的目的

相反,如果这个功能是正确的:

name should contain 'The Walt Disney Company'
market contain 'NYSE'
initial contain 'DIS'
and stock contain '61.27B

我对addComp函数做了什么错误?是否有必要使用令牌将这些数据读入二叉树?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

scanf()格式说明符格式%s更改为%[^;];

"%s"忽略前导空格,然后扫描并保存非空白文本,最后添加NUL。 "%s"将“目标”保存在“目标公司”中 - 不是你想要的。

" %[^;];"忽略前导空格(' '部分),然后扫描&amp;保存非';'文本,然后扫描';',但不保存。 " %[^;];"将“Target Corporation”保存在“Target Corporation”中

还建议您查看sscanf()

的结果
if (4 != sscanf(buffer, " %[^;];  %s  %s  %f " ...) {
  ; // handle error
}