尝试将数据从输入文件读取到二叉树时,addStu 函数出现运行时错误,格式或指针使用有什么问题吗?
这是我读取文件的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "BST_ADT.h"
// Structure
typedef struct
{
char* name;
char* market;
char* initial;
float stock;
}STUDENT;
// Prototype Delarations
void addStu (BST_TREE* list);
//void deleteStu (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);
addStu(list);
deleteStu(list);
findStu (list);
printList(list);
return 0;
}
/*===================== addStu =========================*/
void addStu (BST_TREE* list)
{
// Local Declarations
STUDENT* stuPtr;
FILE* fp;
char fileName[25];
char buffer [100];
// Statements
stuPtr = (STUDENT*)malloc (sizeof (STUDENT));
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));;
} // end while
BST_Insert(list, stuPtr);
} //addStu
这是我的输入:
Microsoft Corporation; NASDAQ MSFT 259.94B
Yahoo! Inc.; NASDAQ YHOO 37.67B
Baidu.com, Inc. (ADR); NASDAQ BIDU 8.64B
CNET Networks, Inc.; NASDAQ CNET 1.13B
QuickLogic Corporation; NASDAQ QUIK 88.62M
Answers Corporation; NASDAQ ANSW 53.49M
Apple Inc.; NASDAQ AAPL 114.17B
EarthLink, Inc.; NASDAQ ELNK 890.68M
Amazon.com, Inc.; NASDAQ AMZN 28.28B
IAC/InterActiveCorp; NASDAQ IACI 6.09B
Time Warner Inc.; NYSE TWX 57.29B