0% found this document useful (0 votes)
10 views4 pages

File Handling in C: Functions & Examples

Chapter 4 covers file handling in C, detailing file types, modes, and essential functions like fopen(), fclose(), fprintf(), and fscanf(). It explains the differences between text and binary files, as well as random access methods using fseek(), ftell(), and rewind(). Additionally, it includes example programs for writing, reading, and copying file content.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

File Handling in C: Functions & Examples

Chapter 4 covers file handling in C, detailing file types, modes, and essential functions like fopen(), fclose(), fprintf(), and fscanf(). It explains the differences between text and binary files, as well as random access methods using fseek(), ftell(), and rewind(). Additionally, it includes example programs for writing, reading, and copying file content.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Chapter 4: File Handling

1 Mark Questions

What is a file?
→ A file is a collection of data or information stored permanently on a storage
device.

Name file opening modes.


→ r, w, a, r+, w+, a+, rb, wb, ab, rb+, wb+, ab+

Function to open file.


→ fopen()

Function to close file.


→ fclose()

Expand EOF.
→ End of File

2 Marks Questions

Difference between text file and binary file.


Text File Binary File
Stores data in readable form Stores data in binary form
Extension is .txt Extension is .bin
Uses fputs(), fgets() Uses fwrite(), fread()

Explain fprintf() and fscanf().


fprintf() → Used to write formatted data into a file.
fscanf() → Used to read formatted data from a file.

Explain fgetc() and fputc().


fgetc() → Reads a single character from file.
fputc() → Writes a single character to file.

Explain r, w, a modes.
r → Open file for reading.

w → Open file for writing (creates new file or overwrites).

a → Open file for appending data.

What is File Pointer?


→ File pointer is a pointer of type FILE used to access and manipulate files in C.

3 Marks Questions
Program to write data in file.
#include<stdio.h>
void main() {
FILE *fp;
fp = fopen("[Link]","w");
fprintf(fp, "Hello Students");
fclose(fp);
}

Program to read data from file.


#include<stdio.h>
void main() {
FILE *fp;
char ch;
fp = fopen("[Link]","r");
while((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
}

Explain random access in file.


→ Random access allows direct access to any part of the file using functions like
fseek(), ftell() and rewind().

Program to copy content from one file to another.


#include<stdio.h>
void main() {
FILE *f1, *f2;
char ch;
f1 = fopen("[Link]","r");
f2 = fopen("[Link]","w");
while((ch = fgetc(f1)) != EOF) {
fputc(ch, f2);
}
fclose(f1);
fclose(f2);
}

Difference between fprintf() and fputs()


fprintf() fputs()
Used to write formatted data Used to write string
Accepts format specifiers Does not accept format specifiers

4 Marks Questions
Explain file handling functions.
fopen() → Opens file

fclose() → Closes file

fprintf() → Writes formatted data

fscanf() → Reads formatted data


fputc() → Writes character

fgetc() → Reads character

fwrite() → Writes binary data

fread() → Reads binary data

Program to store student details in file.


#include<stdio.h>
void main() {
FILE *fp;
char name[20];
int roll;
fp = fopen("[Link]","w");
printf("Enter Name and Roll No: ");
scanf("%s %d", name, &roll);
fprintf(fp, "Name: %s\nRoll No: %d", name, roll);
fclose(fp);
}

Explain random access functions.


fseek(fp, offset, position) → Moves file pointer.

ftell(fp) → Returns current position of file pointer.

rewind(fp) → Resets file pointer to beginning of file.

Program to read and write content in file.


#include<stdio.h>
void main() {
FILE *fp;
char str[50];
fp = fopen("[Link]","w");
printf("Enter Text: ");
gets(str);
fputs(str, fp);
fclose(fp);

fp = fopen("[Link]","r");
while(fgets(str, 50, fp)) {
printf("%s", str);
}
fclose(fp);
}

Explain types of files with example.


Type Description Example
Text File Stores data in readable form .txt file
Binary File Stores data in binary format .bin file

You might also like