files1
files1
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:
#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:
#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:
Example:
#include <stdio.h>
void main(){
FILE *fp;
fp = fopen("myfile.txt","w+");
fputs("This is abcdefg", fp);
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:
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
#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);
}
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
}
Output:
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.
#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:
The fputc() function is used to write a single character into file. It outputs
a character to a stream.
Syntax:
#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:
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