delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/21/12:52:02

Xref: news2.mv.net comp.os.msdos.djgpp:4093
From: demerre AT pluto DOT cs DOT kuleuven DOT ac DOT be (Dieter Demerre)
Newsgroups: comp.os.msdos.djgpp
Subject: Bug in fstream.seek?(0,ios::end) for ASCII-mode
Date: 21 May 1996 14:31:26 GMT
Organization: Department of Computer Science, K.U.Leuven
Lines: 50
Distribution: world
Message-ID: <4nsk3u$7f1@idefix.CS.kuleuven.ac.be>
NNTP-Posting-Host: pluto.cs.kuleuven.ac.be
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hoi, 
Opening a file in Text-mode, and going to the end (to read or write),
causes trouble.  For each \n in the opened file, the pointer jumps
1 byte too far in the file (over the EOF).
The problem does not occur when you open the file in binary-mode.

Example program:
// ---------------------------------------------------------------
// Author: Dieter Demerre
// Project: DJGPP - bug demo
// Filename: POSBUG.CC
// Version: n.a.
// ---------------------------------------------------------------
// DESCRIPTION:
// This file opens itself (POSBUG.CC) in text-mode, and goes to the
// last byte of it.
// There it goest back one byte and reads it (this should be the
// last byte of the file (probably a 0x10)).  It also displays teh
// current position in the file (tellg).  The printed value does not
// corresponds to the filelength.  
// ----------------------------------------------------------------
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main ()
{
	fstream f;
	char c;
	f.open("posbug.cc",ios::in);
	if (!f.good()) {
		cerr << "file \"posbug.cc\" does not exist" << endl;
		exit (-1);
	};
	f.seekg(0,ios::end);
	cout << "Current position in the file == " << f.tellg() << endl;
	f.seekg(-1,ios::cur);
	f >> c;
	f.get(c);
	cout << "Last character in the file == '" << c << "'." << endl;
	f.close();
	return 0;
};

--
Greetings from = Groetjes vanwege
--------------------------------------------
Dieter Demerre
Email : Dieter DOT Demerre AT cs DOT kuleuven DOT ac DOT be
http  : www.cs.kuleuven.ac.be/~demerre
--------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019