READING A FILE IN C IS VERY SIMPLE ALTHOUGH COMPLECTED FOR NEWBIES IN THIS BLOG YOU WILL LEARN THE CONCEPTS IN SWEET AND SHORT MANNER. SO WITHOUT WASTING TIME LET'S DIVE INTO THE CORE CONCEPT :-


It's a saying that if we try to understand things by ourselves before we try understand it by our instructor than it create much better level of understanding of the concepts in our mind so keeping this in mind I will first give you the code than give it some time to understand it :)

Q. Write a program in C to write multiple lines in a text file.
Test Data :
Input the number of lines to be written : 4
:: The lines are ::
test line 1
test line 2
test line 3
test line 4
Expected Output :

 The content of the file test.txt is  :                                                                       
test line 1                                                                                                   
test line 2                                                                                                   
test line 3                                                                                                   
test line 4

Ans.
😊***********😊code without any comments try to understand😊********😊
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char filename[20];
char strc,str[100];
int num1;
printf("please enter file name");
scanf("%s", filename);
printf("enter total no. of lines you want to write");
scanf("%d",&num1);
fp=fopen(filename,"a");
if(fp==NULL)
{
printf("some error occured during opening of file");
exit(0);
}
printf("---------------------------------------\n");
printf("please enter information in the file:-\n");
for(int i=0;i<=num1;i++)
{
fgets(str, sizeof str, stdin);
fputs(str,fp);

}
fclose(fp);
fp=fopen(filename,"r");

printf("THE CONTENT OF THE FILE IS :-\n");
strc=fgetc(fp);
while(strc!=EOF)
{
printf("%c",strc);
strc=fgetc(fp);
}
fclose(fp);
}


😊***********😊code with detailed comments 😊********😊
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp; //file pointer to point toward the file in which we wants to write the data
char filename[20];//declaring the file
char strc;//this is simply a variable which will store the character
printf("please enter file name");
scanf("%s", filename);
fp=fopen(filename,"r");// *r* is for reading mode and *fopen* opens the file where *fp* points to this file
if(fp==NULL)// this condition is used in case like if the file is saved in a cd drive
// in which we cannot write data so in such cases this code will run for more details visit www.google.com
{
printf("some error occured during opening of file");
exit(0);//make sure that we write exit(0) and not exit(1) or exit(2)
}
printf("please read the file carefully\n");
strc=fgetc(fp);// here fgetc() is a function which reads a character from the the file and hence the name f-get-c
while(strc!=EOF)//EOF is known as end of file
{
printf("%c",strc);
strc=fgetc(fp);
}
fclose(fp);//to close the file
}
THANKS FOR VISITING 😍 πŸ‘
FOR ANY DOUBT ASK IN COMMENT SECTION :)

Comments