If you compile your code ("RUN" button) it tells you where are any compilation problems and what the issue is.
Your code has so many problems that listing all of them and proposing a fix one by one is very inconvenient. Instead, I rewrote your code and provide you with a generic list of problems (probably I will miss a few of the issues).
List of problems
- mismatched curly braces
- mismatched parenthesis
- missing comma
- address of (&) operator used when not needed
- extra semicolon at the end of the line for loops (it makes the loop do nothing)
- mismatched fopen()/fclose()
- return in the loop (exiting early, it should have been at the end of the main())
- Using numeric literals instead of the defined values (using 2 instead of SEEK_END)
- The user experience is not too good either. You should tell what you expect of the user (e.g. didn't specify how the search works)
- You should use functions to better structure your code.
A proposed fix
Note: I didn't spend time to validate user input as it would have added even more work.
#include <stdio.h>
#include <string.h>
#define NAME_WIDTH 20
#define CITY_WIDTH 20
#define FEES_WIDTH 10
typedef struct
{
char name[NAME_WIDTH];
char city[CITY_WIDTH];
int fees;
} Student;
FILE * OpenFile(const char * fileName)
{
FILE * file = fopen(fileName, "rb+");
if (file == NULL)
file = fopen(fileName,"wb+");
return file;
}
void PrintHeader()
{
printf("\n");
printf("%-*s%-*s%-*s\n", NAME_WIDTH, "Name", CITY_WIDTH, "City", FEES_WIDTH, "Fees");
for (int i = 0; i < NAME_WIDTH + CITY_WIDTH + FEES_WIDTH; i++)
printf("-");
printf("\n");
}
void PrintFooter(size_t counter)
{
printf("\n");
if (counter == 0)
printf("-- No record found. --\n");
else
printf("-- %lu records found. --\n", counter);
printf("\n\n");
}
void PrintStudent(const Student * student)
{
printf("%-*s%-*s%-*d\n", NAME_WIDTH, student->name, CITY_WIDTH, student->city, FEES_WIDTH, student->fees);
}
int GetChoice()
{
int choice;
printf("\n");
printf("Please, select an operation\n");
printf("---------------------------\n");
printf(" 1) add new record\n");
printf(" 2) list record\n");
printf(" 3) search by name\n");
printf(" 4) exit\n");
printf("? ");
scanf("%d", &choice);
return choice;
}
void AddNewRecord(FILE * file)
{
printf("\nPlease, enter a record: ([name] [city] [fees]): ");
Student student = { };
scanf("%s %s %d", student.name, student.city, &student.fees);
fseek(file, 0, SEEK_END);
fwrite((char*)&student, sizeof(student), 1, file);
}
void ListRecords(FILE * file)
{
rewind(file);
Student student;
size_t counter = 0;
while (fread((char*)&student, sizeof(student), 1, file))
{
if (counter == 0)
PrintHeader();
PrintStudent(&student);
counter++;
}
PrintFooter(counter);
}
void SearchRecords(FILE * file)
{
printf("\nPlease, enter the [name] to look for: ");
char name[NAME_WIDTH];
scanf("%s", name);
rewind(file);
Student student;
size_t counter = 0;
while (fread((char*)&student, sizeof(student), 1, file))
{
if (strcmp(name, student.name) == 0)
{
PrintHeader();
PrintStudent(&student);
counter++;
// assuming multiple students with the same name exists. If not, we should break here.
}
}
PrintFooter(counter);
}
int main()
{
const char * fileName = "aman";
FILE * file = OpenFile(fileName);
if (file == NULL)
{
fprintf(stderr, "Failed to open file: %s\n", fileName);
return -1;
}
int choice;
do
{
choice = GetChoice();
switch (choice)
{
case 1: AddNewRecord(file); break;
case 2: ListRecords(file); break;
case 3: SearchRecords(file); break;
}
} while (choice != 4);
fclose(file);
return 0;
}