KEMBAR78
PPS File Handling Answers | PDF
0% found this document useful (0 votes)
6 views4 pages

PPS File Handling Answers

The document explains three directory methods in file handling: mkdir() for creating directories, opendir() and readdir() for opening and reading directory contents, and rmdir() for removing empty directories. It also distinguishes between text files, which are human-readable and stored in ASCII format, and binary files, which are machine-readable and stored in binary format, providing examples for each. Additionally, it includes a comparison table highlighting the differences between text and binary files.

Uploaded by

Aditya Gawali
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)
6 views4 pages

PPS File Handling Answers

The document explains three directory methods in file handling: mkdir() for creating directories, opendir() and readdir() for opening and reading directory contents, and rmdir() for removing empty directories. It also distinguishes between text files, which are human-readable and stored in ASCII format, and binary files, which are machine-readable and stored in binary format, providing examples for each. Additionally, it includes a comparison table highlighting the differences between text and binary files.

Uploaded by

Aditya Gawali
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/ 4

PPS - File Handling Concepts

Q1. Explain any 3 directory methods with example.

1. mkdir() - Make Directory

Purpose: Creates a new directory.

Syntax: int mkdir(const char *pathname, mode_t mode);

Example:

#include <sys/stat.h>

int main() {

mkdir("NewFolder", 0777);

return 0;

2. opendir() and readdir() - Open and Read Directory

Purpose: Used to open a directory and read its contents.

Example:

#include <stdio.h>

#include <dirent.h>

int main() {

DIR *d;

struct dirent *dir;

d = opendir(".");

if (d) {

while ((dir = readdir(d)) != NULL) {

printf("%s\n", dir->d_name);

}
closedir(d);

return 0;

3. rmdir() - Remove Directory

Purpose: Deletes an empty directory.

Example:

#include <unistd.h>

int main() {

rmdir("NewFolder");

return 0;

Q2. Explain text and binary files with example.

Text Files:

- Stores data in human-readable ASCII format.

- Example:

#include <stdio.h>

int main() {

FILE *fptr;

fptr = fopen("data.txt", "w");

fprintf(fptr, "Hello, this is a text file.\n");

fclose(fptr);

char ch;

fptr = fopen("data.txt", "r");


while ((ch = fgetc(fptr)) != EOF) putchar(ch);

fclose(fptr);

return 0;

Binary Files:

- Stores data in machine-readable binary format.

- Example:

#include <stdio.h>

struct student { char name[20]; int age; };

int main() {

struct student s1 = {"Rahul", 20}, s2;

FILE *fptr = fopen("student.dat", "wb");

fwrite(&s1, sizeof(s1), 1, fptr);

fclose(fptr);

fptr = fopen("student.dat", "rb");

fread(&s2, sizeof(s2), 1, fptr);

fclose(fptr);

printf("Name: %s\nAge: %d\n", s2.name, s2.age);

return 0;

Difference Table:

Text File vs Binary File

- Human-readable vs Machine-readable

- Larger in size vs Compact

- Can be opened manually vs Cannot be opened manually


- Example: .txt vs .dat

You might also like