0

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

这是我的代码:

#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 函数我做错了什么?是否需要使用令牌才能将这些数据读入二叉树?

提前感谢您的帮助

4

1 回答 1

0

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

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

" %[^;];"忽略前导空格(' '部分),然后扫描并保存非';'文本,然后扫描 a ';',但不保存它。 " %[^;];"在“目标公司”中保存“目标公司”;

还建议检查结果sscanf()

if (4 != sscanf(buffer, " %[^;];  %s  %s  %f " ...) {
  ; // handle error
}
于 2013-08-13T19:12:54.900 回答