FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
115 views
in RTU/BTU B.Tech(CSE-I & II Sem) Computer Programming Lab by Goeduhub's Expert (7.6k points)
Programs to implement file handling functions

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

File handling functions 2

fgetc() : The fgetc() function returns a single character from the file. It gets a character from the stream. It returns EOF at the end of file.

Syntax: int fgetc(FILE *stream)  

 Example : 

#include <stdlib.h> 

#include <stdio.h> 

void main() 

{   

FILE *fp;  

char c;    

fp=fopen("C:\\Users\\HP\\Desktop\\abc.txt","r");  

while((c=fgetc(fp))!=EOF){  

printf("%c",c);  

}  

fclose(fp);      

}

Output  : GoEduHub Technologies 

fputs() :The fputs() function writes a line of characters into file. It outputs string to a stream.

Syntax: int fputs(const char *s, FILE *stream)

Example : 

#include <stdlib.h> 

#include <stdio.h>    

void main(){  

FILE *fp;  

fp=fopen("C:\\Users\\HP\\Desktop\\abc.txt","w");  

fputs("how can i help you",fp);    

fclose(fp);   

 Output abc.txt file : how can i help you

fgets() : The fgets() function reads a line of characters from file. It gets string from a stream.

Syntax: char* fgets(char *s, int n, FILE *stream)

 Example : 

#include <stdlib.h> 

#include <stdio.h>    

void main(){  

FILE *fp;  

char text[300];    

fp=fopen("C:\\Users\\HP\\Desktop\\abc.txt","r");  

printf("%s",fgets(text,200,fp));    

fclose(fp);    

}    

  Output : how can i help you

fseek() :The fseek() function is used to set the file pointer to the specified offset. It is used to write data into file at desired location.

Syntax: int fseek(FILE *stream, long int offset, int whence)  

Example :  

#include <stdlib.h> 

#include <stdio.h>    

void main(){  

FILE *fp;   

   fp = fopen("C:\\Users\\HP\\Desktop\\abc.txt","w+");  

   fputs("GoEduHub Technologies Welcomes you : ", fp);  

   fseek( fp, 7, SEEK_SET );  

   fputs("ABCDEFGHI", fp);  

   fclose(fp);    

}   

Output : GoEduHuABCDEFGHIogies Welcomes you : 

For more RTU/BTU II Sem Computer Programming Lab  Experiments CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...