Date: Wed, 8 Nov 1995 11:13:09 +0300 From: "Alexander V. Lukyanov" To: eliz AT is DOT elta DOT co DOT il, lav AT yars DOT free DOT net Cc: djgpp AT sun DOT soe DOT clarkson DOT edu, jkmarsha AT socs DOT uts DOT edu DOT au, kagel AT ts1 DOT bloomberg DOT com Subject: Re: CR/LF pairs... NO NO NO! > From: Eli Zaretskii > > On Thu, 2 Nov 1995, Alexander V. Lukyanov wrote: > > > The so called text mode works fine until using fseek/ftell. I saw > > the bug in Borland C 2,3,4. I didn't test it on gcc, but I'm sure, > > that it is there in many other compilers. > > fseek()/ftell() are NOT the portable method of random access to files. > These functions aren't even ANSI. If you want portability, use fgetpos() > and fsetpos(). > > Btw, fseek()/ftell() will also work, if you only call fseek() with values > returned by ftell(), and not with values produced through arithmetics on > byte offsets. > Well, I have just seen sources of libc for djgpp, and I've found, that djgpp has the same bug I described before. Stdio converts text back to raw format to calculate ftell(), and if text in buffer contained plain '\n' then single '\n' will be converted to two characters "\r\n". This is wrong, because '\n' could be get from "\r\n" and from single '\n'. This is why ftell() can tell wrong value. In djgpp, fgetpos and fsetpos are wrappers for fseek and ftell. One my program used ftell to get position in file, read one line, then go back to pos (via fseek) and read the line again. It works on files in MSDOS format, but when I tried it on text file in UNIX format, it cycled on this, because ftell returned wrong offset. --- Alexander Lukyanov lav AT video DOT yars DOT free DOT net