DBMS Assignment 1 23BCP067
DBMS Assignment 1 23BCP067
Theory:
File handling in C is the process of storing and managing data in files on a
disk. It allows programs to create, open, read, write, modify, and delete
files, enabling persistent storage of data even after the program
terminates.
Files are essential for handling large amounts of data e iciently and are
commonly used for tasks such as data logging, configuration storage, and
database-like operations.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int rollNo;
char name[50];
float marks;
} Student;
void insertRecord() {
FILE *file = fopen(FILENAME, "a");
if (file == NULL) {
printf("Error opening file!\n");
return;
}
Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", s.name); // To accept spaces in the name
printf("Enter Marks: ");
scanf("%f", &s.marks);
fclose(file);
}
void retrieveRecords() {
FILE *file = fopen(FILENAME, "r");
if (file == NULL) {
printf("No records found!\n");
return;
}
Student s;
printf("\n--- Student Records ---\n");
while (fscanf(file, "%d %[^\n] %f", &s.rollNo, s.name, &s.marks) != EOF) {
printf("Roll No: %d, Name: %s, Marks: %.2f\n", s.rollNo, s.name, s.marks);
}
fclose(file);
}
void searchRecord() {
FILE *file = fopen(FILENAME, "r");
if (file == NULL) {
printf("No records found!\n");
return;
}
int rollNo;
printf("Enter Roll Number to search: ");
scanf("%d", &rollNo);
Student s;
int found = 0;
while (fscanf(file, "%d %[^\n] %f", &s.rollNo, s.name, &s.marks) != EOF) {
if (s.rollNo == rollNo) {
printf("Record found: Roll No: %d, Name: %s, Marks: %.2f\n", s.rollNo, s.name,
s.marks);
found = 1;
break;
}
}
if (!found) {
printf("Record not found!\n");
}
fclose(file);
}
void updateRecord() {
FILE *file = fopen(FILENAME, "r");
if (file == NULL) {
printf("No records found!\n");
return;
}
int rollNo;
printf("Enter Roll Number to update: ");
scanf("%d", &rollNo);
Student s;
int found = 0;
while (fscanf(file, "%d %[^\n] %f", &s.rollNo, s.name, &s.marks) != EOF) {
if (s.rollNo == rollNo) {
found = 1;
printf("Enter new Name: ");
scanf(" %[^\n]", s.name);
printf("Enter new Marks: ");
scanf("%f", &s.marks);
}
fprintf(tempFile, "%d %s %.2f\n", s.rollNo, s.name, s.marks);
}
fclose(file);
fclose(tempFile);
remove(FILENAME);
rename("temp.txt", FILENAME);
if (found) {
printf("Record updated successfully!\n");
} else {
printf("Record not found!\n");
}
}
void deleteRecord() {
FILE *file = fopen(FILENAME, "r");
if (file == NULL) {
printf("No records found!\n");
return;
}
int rollNo
printf("Enter Roll Number to delete: ");
scanf("%d", &rollNo);
Student s;
int found = 0;
while (fscanf(file, "%d %[^\n] %f", &s.rollNo, s.name, &s.marks) != EOF) {
if (s.rollNo != rollNo) {
fprintf(tempFile, "%d %s %.2f\n", s.rollNo, s.name, s.marks);
} else {
found = 1;
}
}
fclose(file);
fclose(tempFile);
remove(FILENAME);
rename("temp.txt", FILENAME);
if (found) {
printf("Record deleted successfully!\n");
} else {
printf("Record not found!\n");
}
}
int main() {
int choice;
do {
printf("\n--- Student Record Management ---\n");
printf("1. Insert Record\n");
printf("2. Retrieve Records\n");
printf("3. Search Record\n");
printf("4. Update Record\n");
printf("5. Delete Record\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertRecord();
break;
case 2:
retrieveRecords();
break;
case 3:
searchRecord();
break;
case 4:
updateRecord();
break;
case 5:
deleteRecord();
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
OUTPUT:
Procedure
1 Start the Program
Display the main menu with the following options:
o Insert Record
o Retrieve Records
o Search Record
o Update Record
o Delete Record
o Exit
2 Insert Record
Open students.txt in append mode.
Input Roll Number, Name, and Marks.
Append the record to the file.
Close the file and display a success message.
3 Retrieve Records
Open students.txt in read mode.
Display all records (Roll Number, Name, Marks) or "No records found" if the file is
empty.
Close the file.
4 Search Record
Open students.txt in read mode.
Input Roll Number to search.
Display the matching record or "Record not found."
Close the file.
5 Update Record
Open students.txt in read mode and temp.txt in write mode.
Input Roll Number to update.
If a match is found, input updated Name and Marks and write updated record to
temp.txt.
Replace students.txt with temp.txt.
Display appropriate success or error message.
6 Delete Record
Open students.txt in read mode and temp.txt in write mode.
Input Roll Number to delete.
Copy all non-matching records to temp.txt.
Replace students.txt with temp.txt.
Display appropriate success or error message.
7 Exit Program
Exit the program when the "Exit" option is chosen.
Display "Exiting..." before terminating.
8 Error Handling
Ensure proper error handling for file operations
OUTPUT:
Observation and learning:
The program effectively utilizes file operations in C to manage records dynamically, ensuring
data persistence without predefined structures like arrays or linked lists. Temporary files are used
to maintain data integrity during updates or deletions, and robust error handling ensures smooth
operation. The menu-driven interface makes it user-friendly, with modular functions enhancing
code readability and reusability. However, sequential search limits efficiency for large datasets,
highlighting the need for optimization. Overall, the experiment reinforces file handling, modular
programming, and error checking while providing insights into record management and data
persistence in C.
Conclusion:
This experiment demonstrates how to effectively manage simple records using text files in C. It
provides a practical understanding of file handling, modular programming, and user interaction,
serving as a foundation for more advanced data management systems like databases .