www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/03/12/23:52:50

Date: Sun, 12 Mar 95 20:21:42 -0800
From: Daniel Hitt <hitt AT CS DOT Stanford DOT EDU>
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: write() followed by read() doesn't seem to work

Hi djgpp_ers,

I have a program which needs to write() and read() a file.

It doesn't do this correctly under go32.

I've isolated the problem to a small sample program.

This program compiles and runs correctly on Unix (SunOS and NeXT)
but does not run correctly under go32 (although it compiles cleanly).

I'd appreciate any light anyone can throw on the problem.

The details:

HARDWARE:
     386 (AMD, 40 MHz)

SOFTWARE:
     go32 version 1.12.maint3 Copyright (C) 1994 DJ Delorie
     Lowest version I can run is 1.08
     go32.exe build time was Sun Dec 18 16:36:42 1994
     XMS memory available: 3004 Kb
     Swap Space available: 56424 Kb

RESULTS ON UNIX:
     Attempting to read 4096 bytes; file position is 0
     Successful write and read.

RESULTS ON DOS:
     Attempting to read 4096 bytes; file position is 0
     Failed on read() of deleteme.
     Read 1622 bytes; asked for 4096.
     File position, according to lseek(), is 1627.
     File size, according to fstat(), is 4101.

PROGRAM:

     ltst.c (follows:)

/**************************************************************/
/* To test repeated reading and writing to a file.
 * dan hitt, yeeOn lo
 * 95_03_12
 */

#include <stdio.h>
#include <sys/file.h>
#include <sys/stat.h>

#define buf_size 1024
#define file_name "deleteme"
#define open_mode 0644

static void init_buf(float *b,unsigned int cnt) {
  while (cnt) {*b++=cnt--;} /* immaterial garbage only for comparison */
  return;
}
static void open_file(int *fd) {
  *fd=open(file_name,O_RDWR|O_CREAT,open_mode);

  if (*fd<0) {
    fprintf(stdout,"Failed on attempt to open %s.\n",file_name); exit(-1);
  }
  return;
}
static void rewind_to_start(int fd) {
  long val=lseek(fd,0,L_SET);

  if (val) {
    fprintf(stdout,"Failed to lseek() to start of %s.\n",file_name); exit(-1);
  }
  return;
}
static void write_buf(int fd,const char *wbuf,unsigned int cnt) {
  cnt*=sizeof(float);
  if (cnt!=write(fd,wbuf,cnt)) {
    fprintf(stdout,"Failed on write() of %s.\n",file_name); exit(-1);
  }
  return;
}
static unsigned int file_size(int fd) {
  struct stat buf;

  if (fstat(fd,&buf)) {
    fprintf(stdout,"Failed to stat %s to determine its size.\n",file_name);
    return 0;
  }
  return (unsigned int)(buf.st_size);
}
static void read_buf(int fd,char *rbuf,unsigned int cnt) {
  int read_cnt;

  cnt*=sizeof(float);
  fprintf(stdout,"Attempting to read %u bytes; file position is %ld\n",
	  cnt,lseek(fd,0,L_INCR));
  if (cnt!=(read_cnt=read(fd,rbuf,cnt))) {
    fprintf(stdout,"Failed on read() of %s.\n",file_name);
    fprintf(stdout,"Read %d bytes; asked for %u.\n",read_cnt,cnt);
    fprintf(stdout,"File position, according to lseek(), is %ld.\n",
	    lseek(fd,0,L_INCR));
    fprintf(stdout,"File size, according to fstat(), is %u.\n",
	    file_size(fd));
    exit(-1);
  }
  return;
}
void compare_bufs(const float *b0,const float *b1,unsigned int cnt) {
  unsigned int i;

  for (i=0;i<cnt;i++,b0++,b1++)
    if (*b0!=*b1) {
      fprintf(stdout,"Buffers unexpectedly differ at location %u.\n",i);
      exit(-1);
    }
  return;
}
void main(int argc,char **argv) {
  char wbuf[buf_size*sizeof(float)],rbuf[buf_size*sizeof(float)];
  int fd;

  init_buf((float *)wbuf,buf_size);
  open_file(&fd);
  rewind_to_start(fd);
  write_buf(fd,wbuf,buf_size);
  rewind_to_start(fd);
  read_buf(fd,rbuf,buf_size);
  close(fd);
  compare_bufs((float *)wbuf,(float *)rbuf,buf_size);
  fprintf(stdout,"Successful write and read.\n");
  exit(0);
}
/**************************************************************/

MAKEFILE

----------------------------------------------------------------
#!/bin/csh

# This makefile was first created 95_03_12

ltstCFiles = ltst.c

ltstOFiles = $(ltstCFiles:.c=.o)


CFLAGS = -O -g -Wall

.c.o:
	$(CC) $(CFLAGS) -c $*.c -o $*.o

# Compilation instructions:

ltst: $(ltstOFiles)
	$(GCC) -o ltst -O -g -Wall $(ltstOFiles)

----------------------------------------------------------------


Thanks a lot for any guidance anyone can provide.

dan
hitt AT cs DOT stanford DOT edu

- Raw text -


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