I’m using a file system library and I’m trying to create a readline function.
int al_fgetc(ALLEGRO_FILE *f)
Introduced in 5.0.0
Read and return next byte in the given file. Returns EOF on end of file or if an error occurred.
That is the function I’m using from the library. What I want to do is += the resulting char into a std string if it is != EOF which is -1. I’m just not sure if I need to cast it to get the correct result. Will something like this do it:
bool File::readLine( std::string& buff )
{
if(eof() || !isOpen())
{
return false;
}
buff = "";
int c = 0;
while(c = al_fgetc(m_file) != EOF && c != '\n')
{
buff += (char)c;
}
return buff.length() > 0;
}
I’m going to be reading utf-8 from file so I need to make sure this works correctly.
Thanks
Yes, this will work, except you need an extra set of parentheses because the
!=operator has higher precedence than the=operator:The only reason that
fgetcreturnsintinstead ofcharis that there are 257 possible return values: all 256 possible bytes, orEOF, which signals that there’s no more data left in the file. It will always return either 0-255 orEOF, so it’s safe to cast the return value tocharorunsigned charonce you’ve tested it forEOF.