0

Reading a file with the contents "aaaaa" and the char* text returns "".

Following the step execution shows it going through the fp>>text line once before going over and ending. The file opens correctly. Any ideas?

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;
    }
}
ThatSnail
  • 57
  • 1
  • 12
  • Welcome to Stack Overflow! Are you trying to read the entire contents of the file into a character array? There are other ways to do that. http://stackoverflow.com/questions/116038/what-is-the-best-way-to-slurp-a-file-into-a-stdstring-in-c http://stackoverflow.com/questions/195323/what-is-the-most-elegant-way-to-read-a-text-file-with-c – Robᵩ May 17 '12 at 16:26
  • Ahh, thanks! Works perfectly. – ThatSnail May 17 '12 at 23:24

1 Answers1

3

You have already moved the read pointer past the end of the file with the line:

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

You need to reset it back to the beginning. You can do that with:

fp.seekg(0,std::ios::beg);
Chad
  • 17,081
  • 2
  • 40
  • 60
  • Changed and it worked. Using Rob's solution above though because it seems like the original code I was using takes forever to run through the chars. Thanks for the help! – ThatSnail May 17 '12 at 23:29