In this I will tell you how to operate on file . And how to print alphabets in a text file to our program's to output . So without any further delay let's get started . First I will let you know the code then
come to explanation .
Code:
#include <stdio.h>
int main()
{
FILE *ptr;
char ch;
ptr = fopen("alpha.txt", "r");
while (1)
{
ch=fgetc(ptr);
if(ch==EOF){
break;
}
printf("%c", ch);
}
printf("\n");
fclose(ptr);
return 0;
}
Screenshot of text file :
here we write alphabet in txt file
Logic :for our program we need to create a text file . Here I create file name alpha.txt. Here we first make pointer which points towards file ptr. And a variable ch of type character so that it can get character from file in it . And then we open a file using Fopen. And open file in reading mode . And then we run a infinite loop and our only come out form it only when ch is equal to EOF(end of file) and break take him out of loop . And in variable ch we assign character to it with the help of Fgetc function . And input to this function is ptr which points towards file . And after doing all this we close our file by closing pointer .
Output:
Now you can see that our program operates on file as accepted and print every alphabet on file . So I think that it helpful in your work . If you do not want to miss Interesting program then can follow our blog . And can read other useful programs below .
Thanks for reading
No comments:
Post a Comment