Files


    Find the output of following C program

         
1.  #include<stdio.h>
int main()
{
FILE *fptr;
unsigned char ch;
fptr=fopen("ss.txt","w") ;   
fclose(fptr);
return 0;
}

2.  #include<stdio.h>
int main()
{
FILE *fptr;
unsigned char c;
fptr=fopen("ss.txt","r") ;   
c=getc(fptr);
while(c!=EOF)
{
 printf("%c",c);
 c=getc(fptr);
}
fclose(fptr);
return 0;
}

3.  #include<stdio.h>
int main()
{
FILE *fptr;
char c;
fptr=fopen("ss.txt","r") ;   
c=getc(fptr);
while(c!=EOF)
{
 printf("%c",c);
c=getc(fptr);
}
fclose(fptr);
return 0;
}

4.  #include<stdio.h>
int main()
{
FILE *fptr; 
int ch;
fptr=fopen("ss.txt",'w+');
while((ch=fgetc(fptr))!=EOF)       
putchar(ch) ; 
fclose(fptr) ;
return 0;
}

5.  #include<stdio.h>
int main()
{
FILE *fptr; char str[30]; 
fptr=fopen("ss.txt","r");  
while(fgets(str,30,fptr)!=EOF)      
puts(str);
return 0;
}

6.  #include<stdio.h>
int main()
{
char c;
FILE *f1;
f1=fopen("ss.txt","r");
c=getc(f1);
while(c!=EOF)
{
printf("%c",c);
fseek(f1,1,1);
c=getc(f1);
}
return 0;
}



ANSWER


1.    This will carat an empty file named as ss.txt
2.    It goes infinite loop on printing garbage values on the screen.
Hints-ASCII for EOF character is -1
3.    It will print the content of file “ss.txt” if the file doesn’t exist in the current folder produce run time error.
4.    error   (w+ should be written as a string, like “w+”)
5.    Last line of the file “ss.txt” will be displayed infinitely.
Hint-fgets() does not return NULL on end of file or on error, not EOF. The contents of the who file will be displayed and then fgets( ) will return NULL at the end of file, but the loop condition checks for EOF.
6.    It will read the alternate character from the file ”ss.txt” and print on screen.



No comments:

Post a Comment

For any doubt or suggestion you can write here