💾 Archived View for mirrors.apple2.org.za › archive › ground.icaen.uiowa.edu › MiscInfo › Programmin… captured on 2024-07-09 at 05:19:15.

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-


in article 2f367031.0410010338.293078ec@posting.google.com, jalapeno at
jalapeno1@mac.com wrote on 10/1/04 7:38 AM:

> Dan <dan.reisig@dreisig.com> wrote in message
> news:<BD81EC53.907%dan.reisig@dreisig.com>...
>> My first stab at a 'C' program was a file dump utility that displays
>> the contents of any file (including subdirectories) in a hex dump format
>> similar to the monitor memory dump.
>> My question concerns file handling using ORCA/C (although it may be a
>> question concerning GS/OS).
>> My initial attempt opened the file in binary mode, read a block of data
>> from the file (in this case, 384 byte blocks, 24 lines by 16 bytes),
>> and looping through a routine to format the display lines, read the next
>> block, etc. until the end of the file is reached (or the user quits).
>> What I found was that after reading from a file longer than about 1200
>> bytes, the program would repeat part of the first section of the file
>> in the display. This didn't happen on files smaller than that, and I
>> seem to have eliminated the problem by doing a seek to the beginning
>> of the file before the first read.
>> What is going on hear? Any ideas?
> 
> You have a bug in line 47 of your code.
> 
> Of course my clairvoyance isn't as good as it used to be, so I could
> be wrong.
> 
> Post the minimum amount of code that exhibits this behavior, but in
> comp.sys.apple2.programmer, and let's have a look see.
I added the fseek (see comment)

OK, here's a listing of the code:

// DUMP a file to the screen replacing non-printable
// characters with '.'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define lenbuff 64*16
#define pagelen 22

FILE    *fp;
int     i=0, j=0, l=0;
long    ofst=0; 
int    dest1,dest2;
int    src;
char    coffset[9];
char    pause;
char    dot[3]={'.','.',0}, spc[3]={' ',' ',0};
char    curbyte[4];
char    anull[2]={0,0};
size_t    readlen;
char    buffin[lenbuff];
char    ch[4]={' ',' ',' ',0};
char    buffil[76] =
"                  
";
char    outbuf[76];
char    fname[80];

void buildline()
{
   dest2 = 56;
   sprintf(coffset,"%.6X ",ofst);
   for (j=0;(j<16)&&((i+j)<readlen);j++) {
      src=i+j;
      dest1=j*3+8;
      memmove(ch,buffin+src,1);
      sprintf(curbyte,"%.2X ",ch[0]);
      memmove(outbuf+dest1, curbyte, 3);
      if (ch[0] >31 && ch[0] < 127)
     memmove(outbuf+dest2, ch, 1);
      else
     memmove(outbuf+dest2, dot, 1);
      dest2++;
   }
   memmove(outbuf,coffset,6);
}

int main (int argc, char *argv[])
{
   // check for command line args
   if (argc != 2){
      printf("Usage: DUMP <pathname>\n");
      exit(1); }
   }
   else 
      strcpy(fname,argv[1]);

   // open the file in binary mode for reading
   if ((fp = fopen(fname,"rb")) == NULL) {
      printf("Cannot open %s\n",fname);
      exit (1);
   }
   // added to stop duplications
   fseek(fp,(long) 0x00, SEEK_SET);

   while (!feof(fp)) {
      // read lenbuff bytes from the file
      readlen = fread(&buffin,1,lenbuff,fp);
      if (readlen == 0) {
          if (ferror(fp)) {
             printf("Error reading $s\n",fname);
             exit (1);
          }
          break;
       }
       for (i=0;i<readlen;i+=16) {
          strcpy(outbuf,buffil);           // blank the output buffer
          buildline();
          printf("%s\n",outbuf);
          l++;
          if (l>pagelen) {
             pause=getchar();
             if (pause=='q') {
                fclose(fp);
                exit (0);
             }
             l=0;
          }
          ofst +=16;
       }
   }
   fclose(fp);
   printf("\nFinished dumping %s\n",fname);
 exit (0);       
}