C ++使用.getline()从文件读取第一行

时间:2013-04-30 00:08:46

标签: c++

我很难理解如何阅读文件的第一行。我试图读取文件的第一行,然后检查它是否为空。这就是我想出的但仍然没有工作

void buildTree( NodePtr &root, ifstream& input )
    {
        char line [50];
        line= input.getline();

        if ( line ==  NULL )
        {
           root = NULL;
           return;
        }

    }

2 个答案:

答案 0 :(得分:2)

void buildTree( NodePtr &root, ifstream& input )
    {
        char line [50];
        input.getline(line, sizeof line);

        if (strlen(line) == 0)
        {
           root = NULL;
           return;
        }

    }

答案 1 :(得分:0)

我认为你做错了getline()的格式如下。

如果您使用的是char数组:

char buffer[256];
input.getline(buffer, 256);

如果您使用字符串:

string buffer;
getline(input, buffer);