#include #include #include #include #include /* text.tst.c Version 2 */ int main (void) { int fh1, fh2; unsigned int nbytes = 60000, bytesread; char fh1_IN[60000]; fh1 = open( "text.tst.c", O_RDONLY | O_TEXT ); dup2(fh1, 0); close( fh1 ); gets( fh1_IN ); bytesread = strlen(fh1_IN); printf( "read %u bytes from file\n", bytesread ); { unsigned int i; for (i=0; i< bytesread; i++) { printf( "char value: %i\t\t%c\n", fh1_IN[i], fh1_IN[i] ); } } fh1 = open( "text.tst.c", O_RDONLY | O_BINARY ); dup2(fh1, 0); close( fh1 ); /* ** I first tried: lseek( 0, 0, SEEK_SET ) but that didn't help. ** Michael V. Nikolaev suggested using fseek which does ** reset the file descriptor and stream. Note, without this the change ** from O_TEXT to O_BINARY doesn't take effect either. If you remove the ** fseek then the file descriptor doesn't get reset to the beginning of the ** file as it should and the processing mode remains in O_TEXT. ** ** The fseek shouldn't be needed but works around a bug. ** ** I tried adding the close( 0 ) and fclose( stdin ) but that caused the ** fseek function not to work obviously because the stream was closed. ** */ fseek( stdin, 0L, SEEK_SET ); gets( fh1_IN ); bytesread = strlen(fh1_IN); printf( "read %u bytes from file\n", bytesread ); { unsigned int i; for (i=0; i< bytesread; i++) { printf( "char value: %i\t\t%c\n", fh1_IN[i], fh1_IN[i] ); } } }