| |
| Posted by Howard | PermalinkReply |
|
Howard
| Hello this is my first post to any "newsgroup".
I apologize in advance for any misteps.
I have been learning C using the book "The C Primer" (Hancock, Kreiger, Zamir)
and dmc on a p2 running 98 and dmc in a msdos terminal for the past few months. I occasionally boot to Redhat9 and use gcc but usually in 98 and compiling with dmc so I'm not to far from work stuff.
I have had odd problems on several occasions with stdin, not working as expected in codes. Seems like getchar, putchar, scanf, printf would leave a things like \n in stdin so I would have to come up with work-arounds. I am now up to fopen etc. and once again am having a similar problem, this time with EOF, which prompts this post. As yet I haven't found an explanation as to why or what I SHOULD be doing.
Here is an example of what I wound up doing this time. Any thoughts?
Thanks, Howard
-----
#include <stdio.h>
int main()
{
printf(" primer- test17_4.c \nReading, manipulating and writing to and from files \n -using putw() and getw() \n\n");
FILE *file;
int i;
file = fopen("temp", "w");
for(i=0; i < 985; i += 123)
{ putw(i, file); }
fclose(file);
file = fopen("temp", "r");
/* while((i = getw(file)) != EOF) DOESN'T work, loop runs on */
while((i = getw(file)) != 0xffff) /* this catches whatever it is */
{ printf("%6d %4x \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
primer- test17_4.c
Reading, manipulating and writing to and from files
-using putw() and getw()
0 0
123 7b
246 f6
369 171
492 1ec
615 267
738 2e2
861 35d
984 3d8
----- END -----
|