0% found this document useful (0 votes)
6 views

PSIPL10

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 views

PSIPL10

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/ 12

Name Vatsal Panchal

UID no. 2024800084

Experiment No. 10

AIM:
To Implement various operations on files to solve a given problem

Program 1

PROBLEM A publishing company holds in a file details of all the books they publish.
STATEMENT : However, in the future, they wish to maintain two distinct files (i) paperbacks (ii)
hardbacks. Write a program which reads a file containing details of both
paperback and hardback books and creates two files as specified above.
Assume that the first character in each input record indicates if the book is
paperback(p) or hardback(h) or both(b).

PROGRAM: #include <stdio.h>


#include <stdlib.h>

int main()
{
FILE *inputFile, *paperbackFile, *hardbackFile;
char buffer[256];
char type;

inputFile = fopen("books.txt", "r");


if (inputFile == NULL)
{
printf("Error opening input file.\n");
return 1;
}

paperbackFile = fopen("paperback.txt", "w");


if (paperbackFile == NULL)
{
printf("Error opening paperbacks file.\n");
fclose(inputFile);
return 1;
}

hardbackFile = fopen("hardback.txt", "w");


if (hardbackFile == NULL)
{
printf("Error opening hardbacks file.\n");
fclose(inputFile);
fclose(paperbackFile);
return 1;
}

while (fgets(buffer, sizeof(buffer), inputFile) !=


NULL)
{
type = buffer[0];

if (type == 'p' || type == 'b') {


fprintf(paperbackFile, "%s", buffer);
}
if (type == 'h' || type == 'b') {
fprintf(hardbackFile, "%s", buffer);
}
}

fclose(inputFile);
fclose(paperbackFile);
fclose(hardbackFile);
return 0;
}

RESULT:
Program 2

PROBLEM Set up a file containing vehicle records which hold registration number and
STATEMENT : owner information (name and address). Write a program which, given a
vehicle’s registration number, will rapidly retrieve and print the owner
information.

PROGRAM: #include <stdio.h>


#include <string.h>

struct VehicleRecord
{
char registrationNumber[20];
char ownerName[50];
char ownerAddress[100];
};

void createVehicleRecords()
{
FILE *file = fopen("vehicles.txt", "w");
if (file == NULL)
{
printf("Error opening file.\n");
return;
}

struct VehicleRecord vehicles[] =


{
{"MH12AB1234", "Krish", "Pune"},
{"MH14XY5678", "Advait", "Mumbai"},
{"MH01ZZ9999", "Vatsal", "Mumbai"}
};

int n = sizeof(vehicles) / sizeof(vehicles[0]);

for (int i = 0; i < n; i++)


{
fprintf(file, "%s|%s|%s\n",
vehicles[i].registrationNumber, vehicles[i].ownerName,
vehicles[i].ownerAddress);
}

fclose(file);
printf("Vehicle records created successfully.\n");
}

void searchVehicle(char *regNumber)


{
FILE *file = fopen("vehicles.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return;
}

char line[200];
while (fgets(line, sizeof(line), file) != NULL)
{
struct VehicleRecord vehicle;
sscanf(line, "%[^|]|%[^|]|%[^\n]",
vehicle.registrationNumber, vehicle.ownerName,
vehicle.ownerAddress);
if (strcmp(vehicle.registrationNumber, regNumber)
== 0)
{
printf("Owner Name: %s\n", vehicle.ownerName);
printf("Owner Address: %s\n",
vehicle.ownerAddress);
fclose(file);
return;
}
}

printf("Vehicle with registration number %s not


found.\n", regNumber);
fclose(file);
}

int main()
{

createVehicleRecords();
char regNumber[20];
printf("Enter the vehicle's registration number: ");
scanf("%s", regNumber);
searchVehicle(regNumber);

return 0;
}

RESULT:
Program 3

PROBLEM Assume that each time a book is sold in any shop, the ISBN is written to a
STATEMENT: central serial file. At the end of each week, the file is processed to produce a
list of the ISBNs of all different books sold during the week. This list is ordered
by the number of copies sold. Write and test a program which produces this
bestseller list.

PROGRAM: #include <stdio.h>


#include <stdlib.h>
#include <string.h>

struct Book
{
char isbn[20];
int count;
};

int compareBooks(const void *a, const void *b)


{
return ((struct Book*)b)->count - ((struct
Book*)a)->count;
}

int main()
{
FILE *file = fopen("sales.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}

struct Book books[1000];


int bookCount = 0;

char isbn[20];
while (fscanf(file, "%s", isbn) != EOF)
{
int found = 0;
for (int i = 0; i < bookCount; i++)
{
if (strcmp(books[i].isbn, isbn) == 0)
{
books[i].count++;
found = 1;
break;
}
}
if (!found)
{
strcpy(books[bookCount].isbn, isbn);
books[bookCount].count = 1;
bookCount++;
}
}

fclose(file);

qsort(books, bookCount, sizeof(struct Book),


compareBooks);

printf("Bestseller List:\n");
for (int i = 0; i < bookCount; i++)
{
printf("%s: %d copies sold\n", books[i].isbn,
books[i].count);
}
return 0;
}
RESULT:

Program 4

PROBLEM A file EVENTS consists of records of events that have occurred during one
STATEMENT: calendar year. Each record describes one event and starts with a date field
giving the number of the day in the year during which the event occurred. The
file is ordered in ascending order by this day number. A file SELECT consists
of single-field records, the field being a day no. The file too is ordered in
ascending sequence by this field. Write a program which reads these 2 files
and produces a report which shows, for each date in SELECT, the no. of
events which occurred on that date. For example

Day Events

13 12

45 6

..

--

352 4

PROGRAM: #include <stdio.h>


#include <stdlib.h>

struct EventRecord
{
int day;
};

struct SelectRecord
{
int day;
};

int main()
{
FILE *eventsFile, *selectFile;
struct EventRecord event;
struct SelectRecord select;
int eventsCount[366] = {0};

eventsFile = fopen("EVENTS.txt", "r");


if (eventsFile == NULL)
{
printf("Error opening EVENTS file.\n");
return 1;
}

selectFile = fopen("SELECT.txt", "r");


if (selectFile == NULL)
{
printf("Error opening SELECT file.\n");
fclose(eventsFile);
return 1;
}

while (fscanf(eventsFile, "%d", &event.day) != EOF)


{
if (event.day >= 1 && event.day <= 365)
{
eventsCount[event.day]++;
}
}

fclose(eventsFile);

printf("Day Events\n");
while (fscanf(selectFile, "%d", &select.day) != EOF)
{
if (select.day >= 1 && select.day <= 365)
{
printf("%d %d\n", select.day,
eventsCount[select.day]);
}
}

fclose(selectFile);

return 0;
}
RESULT:
CONCLUSION:
In this experiment, we got hands-on experience with file handling by
working through different problems. We learned how to manage files
effectively, from reading and writing data to processing their contents.
Along the way, we explored real-world uses like counting occurrences,
creating reports, and organizing data based on specific criteria.

You might also like