Thanks to visit codestin.com
Credit goes to www.scribd.com

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

C Crud

The document is a C program for a Student Management System that allows users to add, view, update, and delete student information stored in a text file. It defines a Student structure and includes functions for file operations and user interactions through a command-line interface. The program maintains student data such as name, course, date, address, and phone number.

Uploaded by

koropi8732
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)
16 views4 pages

C Crud

The document is a C program for a Student Management System that allows users to add, view, update, and delete student information stored in a text file. It defines a Student structure and includes functions for file operations and user interactions through a command-line interface. The program maintains student data such as name, course, date, address, and phone number.

Uploaded by

koropi8732
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
You are on page 1/ 4

#include <stdio.

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

#define MAX 100

// Structure to hold student information


typedef struct {
char name[MAX];
char course[MAX];
char date[MAX];
char address[MAX];
char phone[MAX];
} Student;

// Function prototypes
void addStudent();
void viewStudents();
void updateStudent();
void deleteStudent();
void saveToFile(Student students[], int count);
void loadFromFile(Student students[], int *count);

int main() {
int choice;
Student students[MAX];
int count = 0;

loadFromFile(students, &count);

while (1) {
printf("\n=== Student Management System ===\n");
printf("1. Add Student\n");
printf("2. View All Students\n");
printf("3. Update Student\n");
printf("4. Delete Student\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addStudent();
break;
case 2:
viewStudents();
break;
case 3:
updateStudent();
break;
case 4:
deleteStudent();
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Try again.\n");
}
}

return 0;
}

void addStudent() {
FILE *fp = fopen("student_data.txt", "a");
if (!fp) {
printf("Error opening file!\n");
return;
}

Student s;
printf("Enter Name: ");
scanf("%s", s.name);
printf("Enter Course: ");
scanf("%s", s.course);
printf("Enter Date (e.g., YYYY-MM-DD): ");
scanf("%s", s.date);
printf("Enter Address: ");
scanf("%s", s.address);
printf("Enter Phone Number: ");
scanf("%s", s.phone);

fprintf(fp, "%s %s %s %s %s\n", s.name, s.course, s.date, s.address, s.phone);


fclose(fp);
printf("Student added successfully!\n");
}

void viewStudents() {
FILE *fp = fopen("student_data.txt", "r");
if (!fp) {
printf("No data found.\n");
return;
}

Student s;
printf("\n=== Student List ===\n");
while (fscanf(fp, "%s %s %s %s %s", s.name, s.course, s.date, s.address,
s.phone) != EOF) {
printf("Name: %s\n", s.name);
printf("Course: %s\n", s.course);
printf("Date: %s\n", s.date);
printf("Address: %s\n", s.address);
printf("Phone: %s\n", s.phone);
printf("--------------------\n");
}
fclose(fp);
}

void updateStudent() {
FILE *fp = fopen("student_data.txt", "r");
FILE *temp = fopen("temp.txt", "w");

if (!fp || !temp) {
printf("Error opening files.\n");
return;
}
char name[100];
printf("Enter the name of the student to update: ");
scanf("%s", name);

Student s;
int found = 0;

while (fscanf(fp, "%s %s %s %s %s", s.name, s.course, s.date, s.address,


s.phone) != EOF) {
if (strcmp(s.name, name) == 0) {
found = 1;
printf("Enter new Course: ");
scanf("%s", s.course);
printf("Enter new Date: ");
scanf("%s", s.date);
printf("Enter new Address: ");
scanf("%s", s.address);
printf("Enter new Phone: ");
scanf("%s", s.phone);
}
fprintf(temp, "%s %s %s %s %s\n", s.name, s.course, s.date, s.address,
s.phone);
}

fclose(fp);
fclose(temp);

remove("student_data.txt");
rename("temp.txt", "student_data.txt");

if (found)
printf("Student updated successfully.\n");
else
printf("Student not found.\n");
}

void deleteStudent() {
FILE *fp = fopen("student_data.txt", "r");
FILE *temp = fopen("temp.txt", "w");

if (!fp || !temp) {
printf("Error opening files.\n");
return;
}

char name[100];
printf("Enter the name of the student to delete: ");
scanf("%s", name);

Student s;
int found = 0;

while (fscanf(fp, "%s %s %s %s %s", s.name, s.course, s.date, s.address,


s.phone) != EOF) {
if (strcmp(s.name, name) != 0) {
fprintf(temp, "%s %s %s %s %s\n", s.name, s.course, s.date, s.address,
s.phone);
} else {
found = 1;
}
}

fclose(fp);
fclose(temp);

remove("student_data.txt");
rename("temp.txt", "student_data.txt");

if (found)
printf("Student deleted successfully.\n");
else
printf("Student not found.\n");
}

void saveToFile(Student students[], int count) {


FILE *fp = fopen("student_data.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %s %s %s %s\n", students[i].name, students[i].course,
students[i].date,
students[i].address, students[i].phone);
}
fclose(fp);
}

void loadFromFile(Student students[], int *count) {


FILE *fp = fopen("student_data.txt", "r");
if (!fp) return;

*count = 0;
while (fscanf(fp, "%s %s %s %s %s", students[*count].name,
students[*count].course,
students[*count].date, students[*count].address,
students[*count].phone) != EOF) {
(*count)++;
}
fclose(fp);
}

You might also like