【问题标题】:ifstream returning empty string?ifstream 返回空字符串?
【发布时间】:2012-05-17 16:12:35
【问题描述】:

读取内容为“aaaaa”的文件,char* 文本返回“”。

在步骤执行之后显示它在结束和结束之前通过 fp>>文本行一次。文件正确打开。有什么想法吗?

char* Load_Wave_File(char *fname)
{
    std::ifstream fp(fname,std::ios::binary);

    std::streampos fsize=0;
    fsize=fp.tellg();
    fp.seekg(0,std::ios::end);
    fsize=fp.tellg()-fsize;

    char* text;
    text=new char[fsize];
    if(fp.is_open())
    {
        while(!fp.eof())
        {
            fp>>text;
        }
        fp.close();
        return text;
    }
    else
    {
        fp.close();
        return false;
    }
}

【问题讨论】:

标签: c++ visual-c++


【解决方案1】:

您已经将读取指针移到文件末尾以下:

fp.seekg(0,std::ios::end);

您需要将其重置为开始。你可以这样做:

fp.seekg(0,std::ios::beg);

【讨论】:

  • 已更改并且有效。使用上面的 Rob 解决方案,因为我使用的原始代码似乎需要永远运行字符。感谢您的帮助!
猜你喜欢
  • 2015-04-20
  • 2020-01-27
  • 2020-07-11
  • 2015-05-25
  • 2011-05-09
  • 1970-01-01
  • 2012-07-20
  • 2011-12-15
  • 2020-11-23
相关资源
最近更新 更多