www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/16/19:55:11

From: kunst AT prl DOT philips DOT nl
Subject: fseek() trouble ?!?
To: noll AT apx00 DOT physik DOT uni-frankfurt DOT de
Date: Wed, 16 Nov 1994 17:38:47 +0100 (MET)
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu (DJGPP users list)

Dirk Noll (noll AT apx00 DOT physik DOT uni-frankfurt DOT de) wrote:
>>>
>>> We like to run a few UNIX programs, which are written in ANSI-C language, 
>>> on a PC (Pentium 586, 60Hz). We tried to use the GNU DJGPP Version 2.6.0
>>> on the PC.  After running the programs we found the following bug: The 
>>> fseek-function did not work when we used the SEEK_CUR option.  Instead of
>>> using the current position in the file, the position jumps to the end. 
>>> 

I (kunst AT prl DOT philips DOT nl) wrote:
>>
>>Did you use fseek() on binary files, or text files ?
>>Remember that fopen() in DJGPP opens files default in 'text' mode.
>>You have to use 'fopen(fname,"rb")' to open a file in binary mode.
>>
>>The test program below tests the fseek() on binary files.
>>Perhaps you could provide a similar program that doesn't work,
>>so that the people working on DJGPP could improve the compiler.
>>You could post this to the DJGPP mailing list.
>>

Dirk Noll (noll AT apx00 DOT physik DOT uni-frankfurt DOT de) replied:
>
> I used the fseek() on binary files and opened them with "rb".
> So this shouldn't be the problem.
> 
> I ran your program on my PC and it didn't work there either.
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> So there might be a problem with the installation.
> My current version is DJGPP 1.12. 


Question to DJGPP users:

The program 'testseek.c' below works fine on my PC (486DX66)
using both DJGPP 1.11m5 and 1.12m2, on DOS 6.0. 
Has any of you (DJGPP users) experienced similar problems as Dirk has ?
Could you compile and test the program, and post non-successful
results to the list (including your DJGPP version etc.) ?

Regards,

Pieter Kunst (kunst AT prl DOT philips DOT nl)

    
=============================== testseek.c ===================================
/*
 * File : testseek.c
 */

#include <stdio.h>
#include <stdlib.h>

#define TMPFILE  "fseek.tmp"
#define NPOINTS  (1<<16)
#define NSEEK    1000

#define RAND  ((((rand()>>7)&0xFF)<<8)|((rand()>>7)&0xFF))

FILE *openfile (const char *fname, const char *mode)
{
  FILE *fp;

  if ((fp = fopen(fname, mode)) == NULL)
  {
    fprintf (stderr, "Unable to open '%s'.\n", fname);
    exit (1);
  }
  return fp;
}

void generate (const char *fname)
{
  FILE *fp;
  unsigned short words[NPOINTS];
  int i;

  fp = openfile (fname, "wb");
  for (i=0; i<NPOINTS; i++) words[i] = i;
  fwrite (words, sizeof(words), 1, fp);
  fclose (fp);
}

void seekerror (const char *mode, unsigned short value, int idx)
{
  fprintf (stderr, "SEEK_%s error at value %d, at %d try!\n", mode, value, idx);
  exit (1);
}

void testset (const char *fname)
{
  FILE *fp;
  long offset;
  unsigned short v, value;
  int i;

  fp = openfile (fname, "rb");
  for (i=0; i<NSEEK; i++)
  {
    value = RAND;
    offset = value<<1;
    fseek (fp, offset, SEEK_SET);
    fread (&v, sizeof(unsigned short), 1, fp);
    if (v != value) seekerror ("SET", value, i);
  }
  fclose (fp);
  printf ("SEEK_SET works fine.\n");
}

void testcur (const char *fname)
{
  FILE *fp;
  long newoffset, curoffset=0, delta;
  unsigned short v, value;
  size_t uss = sizeof(unsigned short);
  int i;

  fp = openfile (fname, "rb");
  for (i=0; i<NSEEK; i++)
  {
    do {
      value = RAND;
      delta = value<<1;
      if (rand() < RAND_MAX/2) delta = -delta;
      newoffset = curoffset + delta;
    } while ((newoffset < 0) || (newoffset >= ((NPOINTS<<1)-uss)));
    curoffset = newoffset;
    value = curoffset>>1;
    /* printf ("delta = %ld, offset = %ld\n", delta, curoffset); */
    fseek (fp, delta, SEEK_CUR);
    fread (&v, uss, 1, fp);
    curoffset += uss;
    if (v != value) seekerror ("CUR", value, i);
  }
  fclose (fp);
  printf ("SEEK_CUR works fine.\n");
}

void testend (const char *fname)
{
  FILE *fp;
  long lvalue, delta;
  unsigned short v, value;
  int i;

  fp = openfile (fname, "rb");
  for (i=0; i<NSEEK; i++)
  {
    lvalue = 1L + RAND;          /* range: 1..65536 */
    delta = -(lvalue<<1);        /* range: -131072 .. -2 */
    value = NPOINTS - lvalue;    /* range: 0..65535 */
    fseek (fp, delta, SEEK_END);
    fread (&v, sizeof(unsigned short), 1, fp);
    if (v != value) seekerror ("END", value, i);
  }
  fclose (fp);
  printf ("SEEK_END works fine.\n");
}

int main()
{
  generate (TMPFILE);
  testset (TMPFILE);
  testcur (TMPFILE);
  testend (TMPFILE);
  remove (TMPFILE);
  return 0;
}
=============================== testseek.c ===================================

- Raw text -


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