C++/avoiding premature eof termination

QuestionI am having a problem using "while (!File1.eof())" on a program i wrote which does text compression. C++ apparently decides the end of the file has been reached when it sees character 26 (00011010). How can i get C++ to continue reading past this character if it comes up in the middle of my file? Is there a way i can get the byte length of a file when i open it so i can just continue to read the file byte by byte until i reach the end? Any help would be appreciated.

AnswerFrom this I deduce you are using a Microsoft operating system. On MS operating systems - dating from the MS DOS days - <ctrl>Z - ASCII character 1A hexadecimal or 26 decimal to you and me - is the end of file marker for files opened as text files. Also in this mode reading <cr><lf> ("\r\n") translates to _only_ <lf> ('\n') in your code and writing <lf> ('\n') is translated to <cr><lf> ("\r\n") in the file.

To get around this problem you open the file as a binary file using std::ios::binary as one of the open mode flags passed either to the constructor of the file stream or to its open member function. In fact ios is a typedef for basic_ios<char> and basic_ios derives from ios_base, which is where the openmode values are defined - see sections 27.2, 27.4.2 and 27.4.4 of the C++ standard:

For the C library functions you use mode letter 'b' in the open mode string passed to fopen or use the O_BINARY flag when opening a file using open.

In binary mode no special character indicates file end and no translation of end of line sequences is performed, which is what you would want you if you are _decompressing_ the file. Neither of the text mode behaviours are dangerous when compressing text, although the line end translation may not be what you want, and assumes you have no <ctrl>Z characters in your uncompressed text.

Text mode is the default file mode of C and C++ file streams. On some systems there is no difference between text and binary modes - UN*X and Linux for example. For MSVC++ you can change this default behaviour by linking with binmode.obj, or by setting the _fmode global variable to _O_BINARY directly.

For further details on text and binary mode on MS Windows see the MSDN library (online at http://www.msdn.microsoft.com/), the following are good starting points:

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines.
Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is).
When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD.
My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes.
I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.