www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/01/04:37:03

From: zvrba AT jagor DOT srce DOT hr (Zeljko Vrba)
Newsgroups: comp.os.msdos.djgpp
Subject: ftell doesn't work!
Date: 1 Oct 1996 05:22:27 GMT
Organization: Public host at University Computing Centre, Zagreb, CROATIA
Lines: 50
Distribution: world
Message-ID: <52q9qj$9sn@bagan.srce.hr>
NNTP-Posting-Host: jagor.srce.hr
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

ftell() doesn't work on buffered files!!!

Code fragment:


struct index_rec {
  char problem[160], fname[16];
  fpos_t fpos;
};

FILE *indexfile;

void process_file(char *fn) {
FILE *t;
struct index_rec i;

  if(!(t=fopen(fn, "r"))) {
    perror(fn);
  }
  setbuf(t, NULL);
  gotoxy(1, wherey());
  cprintf("\nProcessing: %s", fn);
  strcpy(i.fname, fn);
  while(!feof(t)) {
    memset(i.problem, 0, 160);
    i.fpos=ftell(t);
    gotoxy(30, wherey());
    cprintf("%d characters processed", i.fpos);
    fgets(i.problem, 160, t);
    if(strstr(i.problem, ".p <==")!=NULL)
      fwrite(&i, sizeof(struct index_rec), 1, indexfile);
  }
  fclose(t);
}
/* main isn't relevant */

This code fragment writes all file offsets of lines that contain '.p <=='
string. If setbuf() is commented, it reports a negative (i.e. very large
unsigned positive) offset on a 3000 file bytes long that contains only
two such lines.

If buffering is set to NULL, it works OK.

The program has been tested on BorlandC++ and it works OK, whether
buffering is active or not.

Has anybody else experienced this problem? How to work around it --
unbuffered files are VERY slow!

-- zvrba AT jagor DOT srce DOT hr

- Raw text -


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