Reputation: 21
What is wrong with the program?
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
main( )
{
char * buf="robot.c";
char c;int i=0;
FILE*fp=fopen(buf,"r");
if(!fp)
printf("open error\n");
while(1)
{
//printf("size%c\n",*fp->_IO_read_ptr);
//while((c=getc(fp))>0)
c=getc(fp);
printf("%c",c);
//getc(fp);
//printf("new size%c\n",*fp->_IO_read_end);
for(;i<1000000000;i++);
}
}
Upvotes: 2
Views: 587
Reputation: 58271
As @Barmar noticed, you don't check and break the while loop when EOF is found, so it printf garbage.
Instead, write your while like:
while((c=getc(fp))!=EOF){
printf("%c",c);
for(;i<1000000000;i++);
}
note: c
must be an int in order to hold an EOF-value.
A very good Read: Definition of EOF and how to use it effectively
Upvotes: 6