Unit 5 - Part B 16 Marks
Unit 5 - Part B 16 Marks
1 Explain the different file operations in C with their functionalities and example. (16 Marks)
(Or)
&
Summarize the different file types and file opening modes in C with their functionalities and example
3 (16 Marks )
File Definition
o A File is a collection of data stored permanently.
o FILE data type is used to handle files in C
o Defined in <stdio.h>
o Supports both sequential and random access.
Different File Types
File Operations
Open File fopen() Opens a file in the specified mode. FILE *fp = fopen("data.txt", "r")
fscanf()
Read File fgets() Reads data from file fscanf(fp, "%s", str);
fread()
fprintf()
Write File fputs() Writes data to file fprintf(fp, "Hello");
fwrite()
Close File fclose() Closes the file fclose(fp);
fseek()
File Used for random access and file
ftell() fseek(fp, 0, SEEK_SET);
Positioning pointer manipulation.
rewind()
Example
Develop a C program to copy the contents of one file to another file.
Or
Develop a C program to merge the contents of two text files into a third file.
#include <stdio.h>
int main()
{
FILE *f1, *f2, *f3;
char ch;
fclose(f1);
fclose(f2);
fclose(f1);
fclose(f2);
fclose(f3);
return 0;
}
*****************