0% found this document useful (0 votes)
4 views3 pages

Unit 5 - Part B 16 Marks

The document explains file operations in C, detailing different file types (text and binary) and file opening modes (e.g., read, write, append). It describes functionalities of various file operations such as opening, reading, writing, and closing files, along with examples. Additionally, it provides a sample C program for copying and merging files.

Uploaded by

tomjerry770750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Unit 5 - Part B 16 Marks

The document explains file operations in C, detailing different file types (text and binary) and file opening modes (e.g., read, write, append). It describes functionalities of various file operations such as opening, reading, writing, and closing files, along with examples. Additionally, it provides a sample C program for copying and merging files.

Uploaded by

tomjerry770750
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

20 x 8 = 160 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 Type Description Example


Text file Stores data as readable characters(ASCII) data.txt
Binary file Stores data in machine-readable format. data.bin

Different File Modes

File Mode Description Example


r Open for reading fopen("data.txt", "r")
w Open for writing fopen("data.txt", "w")
a Open for appending, added at the end of the file. fopen("data.txt", "a")
r+ Open for reading and writing. fopen("data.txt", "r+")
w+ Open for reading and writing. fopen("data.txt", "w+")
a+ Open for reading and appending fopen("data.txt", "a+")
rb , rw Same as above, but in binary mode. fopen("data.txt", "rb")

File Operations

Operation Function Description Example

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;

// Step 1: Copy file1.txt to file2.txt


f1 = fopen("file1.txt", "r");
f2 = fopen("file2.txt", "w");

while ((ch = fgetc(f1)) != EOF)


fputc(ch, f2);

fclose(f1);
fclose(f2);

// Step 2: Merge file1.txt and file2.txt into merged.txt


f1 = fopen("file1.txt", "r");
f2 = fopen("file2.txt", "r");
f3 = fopen("merged.txt", "w");

while ((ch = fgetc(f1)) != EOF)


fputc(ch, f3);

while ((ch = fgetc(f2)) != EOF)


fputc(ch, f3);

fclose(f1);
fclose(f2);
fclose(f3);

printf("Copied file1.txt to file2.txt and merged into merged.txt\n");

return 0;
}

*****************

You might also like