Why cant I print upto 1000 in the program below?
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
int n,i;
fp=fopen("aritraa.txt","w");
for(i=0;i<1000;i++)
{
putw(i,fp);
}
fclose(fp);
printf("Displaying the numbers............\n");
fp=fopen("aritraa.txt","r");
while((n=getw(fp))!=EOF)
{
printf("%d\t",n);
}
fclose(fp);
return 0;
}
Marco Alka
Software Engineer, Technical Consultant & Mentor
EOFstands for "End Of File". However your error is not with the EOF, but with the loop which fills the txt file. Have you actually opened it with an editor?for(i=0;i<1000;i++) { putw(i,fp); }Will write all numbers from 0 up until 1000, but not including 1000, so it will not be written to the output file. Your file would only contain the numbers 0-999, and that's what is displayed to you.
This is your code. Compile and execute it and it will do just that.
By changing the above lines to
for(i=0;i<=1000;i++) { putw(i,fp); }...you can have the numbers 0-1000 in output, 1000 included. You can see the changed code here.