简体   繁体   English

getline()与ifstream指针错误?

[英]getline() with ifstream pointer error?

I have the following code: 我有以下代码:

void Category:: fillCategories(char** & categories, char** &subset, std::ifstream *input, 
int*&subIndex)
{
    while(!input->eof())
    {
        char buffer[30];
        if (getCatSize()==getCatCapacty())
        {
            resize(categories, getCategoryCapAddress(), 5);
        }
        if (getSubSize()==getSubCap())
        {
            resize(subset, getSubsetCapAddress(), 5, subIndex);
        }
        std::getline(*input,buffer);
    }
}

for some reason 由于某些原因

std::getline(*input,buffer); 

is giving me an error. 给我一个错误。 Is there anything I can do to fix this? 有什么我可以解决的吗?

You should move getline up into the loop condition, and replace while with for , like this: 您应该将getline向上移动到循环条件中,并用for替换while ,如下所示:

for (std::string buffer; getline(*input, buffer); )

And of course remove the char[] version of buffer . 当然,删除char[]版本的buffer This way you have a dynamically sized buffer that works with std::getline and uses the correct termination condition. 这样,您就有了一个动态大小的buffer ,该buffer可与std::getline使用并使用正确的终止条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM