fputs() and fgets()
The fputs() and fgets() in C programming are used to write and read string
from stream.
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<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
fp=fopen("myfile2.txt","w");
fputs(" c programming",fp);
fclose(fp);
}
fgets() function
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<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char text[300];
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
}
fseek() function
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)
There are 3 constants used in the fseek() function for whence: SEEK_SET,
SEEK_CUR and SEEK_END.
Example:
#include <stdio.h>
void main(){
FILE *fp;
fp = fopen("myfile.txt","w+");
fputs("This is abcdefg", fp);
fseek( fp, 7, SEEK_SET );
fputs("hello c program", fp);
fclose(fp);
}
The FILE *stream argument is a reference to the file's associated FILE
structure. By utilizing the fopen() method, it is acquired.
Long int offset: The offset parameter specifies how many bytes will
relocated to the whence parameter's location. It may be zero, positive,
or negative.
Whence is an integer that indicates the reference place from which the
offset is determined. Any one of the following three constants may be
used:
SEEK_SET: It establishes the offset with respect to the file's start.
SEEK_CUR: It modifies the file pointer's offset to its current location.
SEEK_END: It establishes the offset with respect to the file's end.
rewind() function
The rewind() function sets the file pointer at the beginning of the stream.
It is useful if you have to use stream many times.
void rewind(FILE *stream)
Example:
File: file.txt
this is a simple text
File: rewind.c
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("file.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
rewind(fp);//moves the file pointer at beginning of the file
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
}
Output:
this is a simple textthis is a simple text
ftell() function
The ftell() function returns the current file position of the specified stream.
We can use ftell() function to get the total size of a file after moving file
pointer at the end of file. We can use SEEK_END constant to move the file
pointer at the end of file.
long int ftell(FILE *stream)
Example:
File: ftell.c
#include <stdio.h>
#include <conio.h>
void main (){
FILE *fp;
int length;
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fclose(fp);
printf("Size of file: %d bytes", length);
}
Output:
Size of file: 21 bytes
fputc() and fgetc()
The fputc() function is used to write a single character into file. It outputs
a character to a stream.
Syntax:
int fputc(int c, FILE *stream)
Example:
#include <stdio.h>
void main(){
FILE *fp;
fp = fopen("file1.txt", "w");//opening file
fputc('a',fp);//writing single character into file
fclose(fp);//closing file }
file1.txt a
fgetc() function
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<stdio.h>
void main(){
FILE *fp;
char c;
fp=fopen("file1.txt ","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
}
this is simple text message
an array of struct type called "employee". The structure has a string,
an integer, and a float element. Using the fprintf() function, the data is written
to a file.
#include <stdio.h>
struct employee {
int age;
float percent;
char *name; };
int main() {
FILE *fp,int ,i,char *string;
struct employee emp[] = {
{25, 65.5, "Ravi"}, {21, 75.5, "Roshan"}, {24, 60.5, "Reena"} };
fp = fopen("file3.txt", "w");
for i = 0; i < 3; i++) {
fprintf(fp, "%d %f %s\n", emp[i].age, emp[i].percent, emp[i].name);
}
fclose(fp);
return 0; }
#include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fp, *fp2, *fp3;
char ch, file1[20], file2[20], file3[20];
printf("Enter name of first file ");
gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name to store merged file ");
gets(file3);
fp1 = fopen(file1, "r");
fp2 = fopen(file2, "r");
if (fp1 == NULL || fp2 == NULL) {
perror("Error has occured");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE); }
fp3 = fopen(file3, "w");
if (fp3 == NULL) {
perror("Error has occures");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE); }
while ((ch = fgetc(fp1)) != EOF)
fputc(ch, fp3);
while ((ch = fgetc(fp2) ) != EOF)
fputc(ch, fp3);
printf("Two files merged %s successfully.\n", file3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0; }
$ cc pgm47.c
$ a.out
Enter name of first file a.txt
Enter name of second file b.txt
Enter name to store merged file merge.txt