/* File Test 1: Read a binary file in arbitrary chunks of bytes. Modify data: Add 1 to each byte and write to output file. Write 1 additional control byte after each chunk. Write 1 additional byte at eof */ #define mywait 1 #include #include #include #include //#include //#include #include //#include //#include #define chunk 7 void main (void) { char inbuffer[chunk],outbuffer[chunk]; char infile_name []="data_in.txt"; char outfile_name []="data_out.txt"; char control_char1='$',control_char2='*'; int i,j,i_ok, nbytes; int fh1, fh2, fl1; unsigned int uk,bytesread,byteswrite,in_done,out_done,in_todo; char c; printf("\nFile Test 1\n\n"); i_ok=0; /* open input file */ /* ================================================== */ fh1 = _open(infile_name,_O_RDONLY | _O_BINARY ); if( fh1 == -1 ) {printf( "Failed to open input file %s\n",infile_name);} else {fl1=_filelength( fh1 ) ; printf( "Input file %s (%d bytes) opened\n",infile_name,fl1); i_ok++; } /* open output file */ if (i_ok) { /* ================================================== */ fh2=_open(outfile_name,_O_RDWR | _O_CREAT | _O_BINARY ,_S_IREAD | _S_IWRITE ); if( fh2 == -1 ) {printf( "Failed to open output file %s\n",outfile_name); i_ok=0;} else {printf( "Output file %s opened\n\n",outfile_name );} } /* init task loop */ in_done=0; out_done=0; nbytes=chunk; /* task loop */ while (i_ok) { /* read a chunk */ in_todo=fl1-in_done; if(int(in_todo)=fl1) { outbuffer[0]=control_char2; _write( fh2, outbuffer, 1); out_done++; i_ok=0;} /* wait for user ok */ printf("\nType any key to continue.\n"); _getch(); } /* end of task loop */ /* finish up */ _close( fh1 ); i = _chsize( fh2, out_done ); if(i<0) {printf("Error when changing filesize\n");} _close( fh2 ); #if mywait /* show console window until key pressed */ printf("\nType any key to exit this program.\n"); _getch(); #endif }