PSIPL10
PSIPL10
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).
int main()
{
FILE *inputFile, *paperbackFile, *hardbackFile;
char buffer[256];
char type;
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.
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;
}
fclose(file);
printf("Vehicle records created successfully.\n");
}
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;
}
}
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.
struct Book
{
char isbn[20];
int count;
};
int main()
{
FILE *file = fopen("sales.txt", "r");
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
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);
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
struct EventRecord
{
int day;
};
struct SelectRecord
{
int day;
};
int main()
{
FILE *eventsFile, *selectFile;
struct EventRecord event;
struct SelectRecord select;
int eventsCount[366] = {0};
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.