File Handling in C – Set 4

In the last instructional exercise, I educated you regarding the document duplicate program utilizing record information yield capacities.

From the last two instructional exercises you more likely than not saw that the base of the program is the same for everything except we are making simply basic bend to do our ideal undertaking. There are boundless potential outcomes utilizing record info yield work. I have just examined two in the last two instructional exercises. Today I will inform you concerning another valuable program to do record information yield utilizing Strings. Also, this will be the last program of this subject.

Record Handling in C

String Input/Output in Files

Till now we are doing info yield by utilizing character. Anyway, in commonsense we regularly use strings rather than characters individually. In this program, I will utilize another capacity whose name is fputs(). Gracious Yes you are correct! This capacity will be utilized to compose a series of characters on records. So let’s construct one program for that.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
void main() 
{  
 FILE *fp;  
 char p[80];
 fp=fopen("demo.txt","w");
 
 if(fp==NULL)  
 {   
  puts("Cannot open file");   
  exit(0);  
 }  
 
 printf("nEnter the text to write on filen");
  
 while(strlen(gets(p))>0)  
 {   
  fputs(p,fp);   
  fputs("n",fp);  
 }    
 
 fclose(fp); 
}

Output

Above program will write strings of characters in the file Demo.txt

Clarification

1). In the beginning, I have announced one FILE pointer fp and string p() whose length is 80 characters.

After that, I have opened the document in “w” mode. This will check the document on the plate. On the off chance that it is available, at that point it will open it else it will make a document with given name and expansion.

In the event that the document can’t be gotten to, at that point it will show a message “can’t open a record”.

After that, I have set one printf() capacity to educate the client to record a few strings.

Presently I have made one endless circle and I have utilized strlen() capacity to leave the program if the client press Enter key multiple times at the same time.

Inside the while circle, I have utilized fputs() capacity to record the strings in the document.

Give us a chance to make one program to perceive how to peruse string from document.

#include<stdio.h>
#include<stdlib.h>
 
void main() 
{  
 FILE *fp;  
 char p[80];
 fp=fopen("demo.txt","r");
 
 if(fp==NULL)  
 {   
  puts("Cannot open file");   
  exit(0);  
 }  
  
 while(fgets(p,'n',fp)!=NULL)  
 {   
  puts(p);   
 }    
 
 fclose(fp); 
}

Output

It will display the content of file demo.txt.

Clarification

I have opened the document demo.txt in reading mode. Presently I am perusing the strings from the document utilizing fgets() work.

As should be obvious I have utilized ‘n’, it implies fgets() will peruse a string till a new line is experienced. When fgets() can’t discover any longer string then it will return NULL and this will be the finish of document.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

13 + 12 =

Recent Content