c++ - 98/msdos/dmc EOF behavior
- Howard (51/51) Feb 03 2007 /*
- Bertel Brander (13/76) Feb 03 2007 I think it's a bug!
- Howard (48/124) Feb 07 2007 Yes that 'seems' to work... but I don't know...
- Bertel Brander (14/18) Feb 07 2007 EOF is just a value, defined in stdio.h as:
- Howard (13/21) Feb 08 2007 Well I think I get it, I'm slow...
/* Hello, I am fairly new to C and am using dmc on win98/msdos terminal. I have been learning C using the book "The C Primer" (Hancock, Kreiger, Zamir) I am having trouble understanding why EOF does not work in a condition check as the book assumes it will and wonder what is causing the failure: 98/dos, dmc, my computer ?? I have come up with a work around but still the question nags. Here is the program from the book I'm trying: ----- */ #include <stdio.h> int main() { FILE *file; int i; printf("%d %x \n",EOF, EOF); /* hmmm, EOF prints ffff ffff */ file = fopen("temp", "w"); for(i=0; i < 985; i += 123) { putw(i, file); } /* with nothing: last output 03d8 ffff*/ fclose(file); file = fopen("temp", "r"); // while((i = getw(file)) != EOF) //DOESN'T work, loop always runs on */ while((i = getw(file)) != 0xffff) /* this does catch it */ { printf("%6d %4x hi-0 \n", i, i); } fclose(file); } /* ===== output ====== D:\C\primer\17_file_io> dmc -A -r -wx 17_4.c link 17_4,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4 D:\C\primer\17_file_io> dmc -A -r -wx 17_4x1.c link 17_4x1,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4x1 -1 ffffffff 0 0 hi-0 123 7b hi-0 246 f6 hi-0 369 171 hi-0 492 1ec hi-0 615 267 hi-0 738 2e2 hi-0 861 35d hi-0 984 3d8 hi-0 -when loop runs on the output continues with: 65535 ffff ...which repeats but ((i = getw(file)) != EOF) doesn't see it right. -I've tried != (int)EOF -no success I'm also into feof section of the book which is working, but I still wonder... Any ideas? */
Feb 03 2007
Howard skrev:/* Hello, I am fairly new to C and am using dmc on win98/msdos terminal. I have been learning C using the book "The C Primer" (Hancock, Kreiger, Zamir) I am having trouble understanding why EOF does not work in a condition check as the book assumes it will and wonder what is causing the failure: 98/dos, dmc, my computer ?? I have come up with a work around but still the question nags. Here is the program from the book I'm trying: ----- */ #include <stdio.h> int main() { FILE *file; int i; printf("%d %x \n",EOF, EOF); /* hmmm, EOF prints ffff ffff */ file = fopen("temp", "w"); for(i=0; i < 985; i += 123) { putw(i, file); } /* with nothing: last output 03d8 ffff*/ fclose(file); file = fopen("temp", "r"); // while((i = getw(file)) != EOF) //DOESN'T work, loop always runs on */ while((i = getw(file)) != 0xffff) /* this does catch it */ { printf("%6d %4x hi-0 \n", i, i); } fclose(file); } /* ===== output ====== D:\C\primer\17_file_io> dmc -A -r -wx 17_4.c link 17_4,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4 D:\C\primer\17_file_io> dmc -A -r -wx 17_4x1.c link 17_4x1,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4x1 -1 ffffffff 0 0 hi-0 123 7b hi-0 246 f6 hi-0 369 171 hi-0 492 1ec hi-0 615 267 hi-0 738 2e2 hi-0 861 35d hi-0 984 3d8 hi-0 -when loop runs on the output continues with: 65535 ffff ...which repeats but ((i = getw(file)) != EOF) doesn't see it right. -I've tried != (int)EOF -no success I'm also into feof section of the book which is working, but I still wonder... Any ideas?I think it's a bug! setw does not return an int, but a short. What it returns on end of file is 0xFFFF not 0xFFFFFFFF. 0xFFFFFFFF is -1, the same as EOF. This seems to work: while((i = (short )getw(file)) != EOF) That is, if you assume that getw returns a short, it will work. -- Just another homepage: http://damb.dk But it's mine - Bertel
Feb 03 2007
Bertel Brander wrote:Howard skrev:Yes that 'seems' to work... but I don't know... (char )getw(file) will work to stop loop too (ff is -1 also) but I sure don't want that output! This has turned into an interesting exercise. I note that putw is also creating shorts. Here's the hex of temp file: 0: 00 00 7b 00 f6 00 71 01 ec 01 67 02 e2 02 5d 03 10: d8 03 ----- If I change the putw loop to: int i; for(i=0x7ffd; i < 0x8003; i += 1) { putw(i, file); } And use: while((i = (short)getw(file)) != EOF) I get a signed short and EOF is caught properly: 32765 7ffd sizeof(getw(file)): 4 32766 7ffe 32767 7fff -32768 ffff8000 -32767 ffff8001 -32766 ffff8002 hex on temp: 0: fd 7f fe 7f ff 7f 00 80 01 80 02 80 ----- using: while((i = getw(file)) != EOF) I get an unsigned short and loop runs on: 32765 7ffd sizeof(getw(file)): 4 32766 7ffe 32767 7fff 32768 8000 32769 8001 32770 8002 65535 ffff 65535 ffff I'm trying to understand (my first mistake) Is what's going on here because the default getw is unsigned short and being an unsigned short the EOF is not expanded from ffff to ffffffff ? Then when it's cast is specified as a short it becomes a signed short and thusly the ffff EOF signal is expanded (being a negative)? Why doesn't EOF look for all three types? ff ffff ffffffff I have tried to (int)putw but get no change to the temp file. If this IS a bug, am I supposed to report it to someone? Do I ask too many questions? BTW, I tried all this in gcc and putw does write 4 byte words in temp file, and getw retuns 4 byte words, and EOF, of course, is caught ok. Sorry for my ignorance, Howard/* Hello, I am fairly new to C and am using dmc on win98/msdos terminal. I have been learning C using the book "The C Primer" (Hancock, Kreiger, Zamir) I am having trouble understanding why EOF does not work in a condition check as the book assumes it will and wonder what is causing the failure: 98/dos, dmc, my computer ?? I have come up with a work around but still the question nags. Here is the program from the book I'm trying: ----- */ #include <stdio.h> int main() { FILE *file; int i; printf("%d %x \n",EOF, EOF); /* hmmm, EOF prints ffff ffff */ file = fopen("temp", "w"); for(i=0; i < 985; i += 123) { putw(i, file); } /* with nothing: last output 03d8 ffff*/ fclose(file); file = fopen("temp", "r"); // while((i = getw(file)) != EOF) //DOESN'T work, loop always runs on */ while((i = getw(file)) != 0xffff) /* this does catch it */ { printf("%6d %4x hi-0 \n", i, i); } fclose(file); } /* ===== output ====== D:\C\primer\17_file_io> dmc -A -r -wx 17_4.c link 17_4,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4 D:\C\primer\17_file_io> dmc -A -r -wx 17_4x1.c link 17_4x1,,,user32+kernel32/noi; D:\C\primer\17_file_io> 17_4x1 -1 ffffffff 0 0 hi-0 123 7b hi-0 246 f6 hi-0 369 171 hi-0 492 1ec hi-0 615 267 hi-0 738 2e2 hi-0 861 35d hi-0 984 3d8 hi-0 -when loop runs on the output continues with: 65535 ffff ...which repeats but ((i = getw(file)) != EOF) doesn't see it right. -I've tried != (int)EOF -no success I'm also into feof section of the book which is working, but I still wonder... Any ideas?I think it's a bug! setw does not return an int, but a short. What it returns on end of file is 0xFFFF not 0xFFFFFFFF. 0xFFFFFFFF is -1, the same as EOF. This seems to work: while((i = (short )getw(file)) != EOF) That is, if you assume that getw returns a short, it will work.
Feb 07 2007
Howard skrev:Why doesn't EOF look for all three types? ff ffff ffffffffEOF is just a value, defined in stdio.h as: #define EOF (-1)I have tried to (int)putw but get no change to the temp file.GetW is defined in stdio.h as: int __CLIB getw(FILE *FHdl); So it should return 0xFFFFFFFF on EOF, it does not, it returns 0xFFFFIf this IS a bug, am I supposed to report it to someone?It IS a BUG, hope that someone at Digital Mars comes around, I don't know how to report errors otherwice.Do I ask too many questions?No. -- Just another homepage: http://damb.dk But it's mine - Bertel
Feb 07 2007
Bertel Brander wrote:Howard skrev:Well I think I get it, I'm slow... For now I'll just accept the dmc bug as: putw and getw handle a short and NOT an int and getw returns an unsigned short (ffff = 65535) so I need to use (short)getw which will cast data as a signed short (ffff = ffffffff = -1) and so will match EOF Hope I've got that concept right. Much thanks for your help in clearing it up for me, HowardWhy doesn't EOF look for all three types? ff ffff ffffffffEOF is just a value, defined in stdio.h as: #define EOF (-1)
Feb 08 2007