ifstream返回空字符串?

时间:2012-05-17 16:12:36

标签: c++ visual-c++

读取内容为“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;
    }
}

1 个答案:

答案 0 :(得分:3)

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

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

您需要将其重置为开头。你可以用:

来做到这一点
fp.seekg(0,std::ios::beg);