您好我有這樣的代碼讀取文本文件轉換成字符串讀取數據文件轉換成字符串中使用字符串標記
void addStu (BST_TREE* list)
{
// Local Declarations
COMPANY* stuPtr;
FILE* fp;
int i = 0;
char fileName[25];
char buffer [100];
char* tempString;
// 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));
tempString = (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);
tempString = buffer;
stuPtr->name = strtok(tempString, ";");
sscanf(buffer, "%15s %15s %f ", stuPtr->market, stuPtr->initial, &(stuPtr->stock));
BST_Insert(list, stuPtr);
i++;
} // end while
} //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
例如,當我試圖讀取第一線,我能得到stuPtr->名使用字符串令牌包含Microsoft Coporation
,這點T之後他tempString將被修改爲包含Microsoft Coporation
以及由於某種原因,該緩衝區沒有被更改,導致stuPtr->market
,stuPtr->initial
和stuPtr->stock
有錯誤的輸入值。
爲什麼緩衝區改變而不被修改?我怎樣才能修復這個代碼,使其工作?
感謝您幫助