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