2014-02-19 69 views
0

好的,所以我應該讀取文件的整行內容,然後分別將每個文件分成正確的分組。這是我迄今爲止編寫的代碼。從文件中讀取時遇到問題

while(!inFile.getline(largeString, LINE_SIZE, '\n').eof() && count < TEAMS) 
    { 
     next = 0; 
     next = buildStruct(largeString, smallString, next); 
     strcpy(stats[count].name, smallString); 
     cout << stats[count].name << endl; 
     next = buildStruct(largeString, smallString, next); 
     stats[count].wins = atoi(smallString); 
     cout << stats[count].wins << endl; 
     next = buildStruct(largeString, smallString, next); 
     stats[count].losses = atoi(smallString); 
     cout << stats[count].losses << endl; 
     next = buildStruct(largeString, smallString, next); 
     stats[count].overtime_losses = atoi(smallString); 
     cout << stats[count].overtime_losses << endl; 
     count++; 
    } 

,這裏是我的文件

Brookings 12 24 7 

    Aberdeen  22 16 2 

Austin 28 11  1 

    Bismark 24 13  4 

    Minot 18 21  3 

的內容,這是我從編譯器

Brookings 
12 
24 
7 

0 
0 
0 
Aberdeen 
22 
16 
2 

我不知道在哪裏零的來源越來越...請幫忙

+0

您的文本文件在每行數據後都有一個空白換行符。你似乎沒有避免線路空白 – sajas

+2

看看[stringstream](http://www.cplusplus.com/reference/sstream/stringstream/stringstream),你會喜歡它。 – Beta

回答

0

在進行處理之前,請檢查largeString是否爲空。

while(!inFile.getline(largeString, LINE_SIZE, '\n').eof() && count < TEAMS)  
{ 
if(largeString && largeString[0] != '\0') 
{ 
next = 0; 
... 
... 
} 
} 

作爲測試版,建議您一定要考慮stringstreams。

相關問題