首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取文件时遇到问题

读取文件时遇到问题
EN

Stack Overflow用户
提问于 2014-02-19 15:18:52
回答 1查看 43关注 0票数 0

好的,我应该读取文件的一整行,然后遍历并将每一块单独划分到正确的分组中。以下是我到目前为止编写的代码。

代码语言:javascript
运行
复制
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++;
    }

这是我的文件的内容

代码语言:javascript
运行
复制
 Brookings  12   24    7

   Aberdeen     22    16 2

 Austin    28   11     1

  Bismark   24   13      4

    Minot  18    21     3

这就是我从编译器得到的东西

代码语言:javascript
运行
复制
Brookings
12
24
7

0
0
0
Aberdeen
22
16
2

我不知道零是从哪里来的..。请帮帮忙

EN

回答 1

Stack Overflow用户

发布于 2014-02-19 15:25:25

在继续处理之前,请检查largeString是否为空。

代码语言:javascript
运行
复制
while(!inFile.getline(largeString, LINE_SIZE, '\n').eof() && count < TEAMS)    
{
if(largeString && largeString[0] != '\0')
{
next = 0;
...
...
}
}

正如Beta,建议你一定要研究一下stringstreams。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21873606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档