2014-02-19 68 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。

相关问题