in article 2f367031.0410010338.293078ec@posting.google.com, jalapeno at jalapeno1@mac.com wrote on 10/1/04 7:38 AM: > Dan wrote in message > news:... >> 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 #include #include #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)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 \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;ipagelen) { pause=getchar(); if (pause=='q') { fclose(fp); exit (0); } l=0; } ofst +=16; } } fclose(fp); printf("\nFinished dumping %s\n",fname); exit (0); }