www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/02/19:32:01

From: benvg AT hotpop DOT com
Newsgroups: comp.os.msdos.djgpp
Subject: fseek()/ fread() troubles
Date: Sun, 02 May 1999 22:49:33 GMT
Organization: Deja News - The Leader in Internet Discussion
Lines: 86
Message-ID: <7gikpt$k2i$1@nnrp1.dejanews.com>
NNTP-Posting-Host: 206.173.94.58
X-Article-Creation-Date: Sun May 02 22:49:33 1999 GMT
X-Http-User-Agent: Mozilla/4.5 [en] (Win95; I)
X-Http-Proxy: 1.0 x16.dejanews.com:80 (Squid/1.1.22) for client 206.173.94.58
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I've been trying to use fseek and fread to do some file i/o but weird things
are happening. I can't seem to figure it out so I wrote this short program,
and maybe someone can help me out.

---problem.c---

/***********************************************************************\
* problem.c -- this program creates a file of STRUCTURE's. It then      *
* reads random structures out of the file to see if fseek/fread works.  *
* When I run it, fseek returns -1 (an error) and fread (or read) don't  *
* get the right number.                                                 *
\***********************************************************************/

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

#define NUM_TESTS      256

/* a dummy structure  (20 bytes) */
typedef struct {
  int number;
  int bob;
  int george;
  int jerome;
  int tom;
} STRUCTURE;

int main(int argc, char **argv) {

  int index;
  int rand_num;
  int fseek_ret;
  int ftell_ret;
  FILE *fp;
  STRUCTURE data;
  char *yn;

  /* generate the file -- this part is not the problem by the way */
  data.number = 0;
  fp = fopen("problem.tmp", "wb");
  for(index = 0; index < 256; index++) {
    write(fileno(fp), &data, 20);
    data.number++;
  }
  fclose(fp);


  printf("+---------+-------+-------+------------+------------+---------+\n");
  printf("| Trial # | fseek | ftell | Expected # | Actual #   | Succes? |\n");
  printf("+---------+-------+-------+------------+------------+---------+\n");

  fp = fopen("problem.tmp", "rb");
  if(fp == NULL)
    exit(0);

  /* now do the tests */
  for(index = 0; index < NUM_TESTS; index++) {
    rand_num = rand() & 0x000000FF;  /* get a number 0...255 */
    fseek_ret = fseek(fp, SEEK_SET, rand_num * 20);
    ftell_ret = ftell(fp);

    /* neither of these work, but they cause different results (?) */
    /* read(fileno(fp), &data, 20); */
    fread(&data, 20, 1, fp);

    if(rand_num == data.number) /* if it worked */
      yn = "yes    ";
    else
      yn = "no     ";
    printf("| %7d | %5d | %5d | %10d | %10d | %s |\n",
           index, fseek_ret, ftell_ret, rand_num, data.number, yn);

  }
  printf("+---------+-------+-------+------------+------------+---------+\n");

  fclose(fp);
  remove("problem.tmp");

  return 0;
}

---problem.c---

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

- Raw text -


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