Search in file handling error

Hello, i would like to ask about how to create search in file handling because whenever i try mine it always have this error of unable to open the file even though the .txt exists.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
FILE *fp;
fp = fopen("list.txt", "a+");
if (!fp)
{
cout << "Cannot open file. \n";
exit(1);
}
struct Sample
{
char fname[255];
char lname[255];
char eno[30];
char salary[30];
};
struct Sample e;
int a;
cout << "Enter 1 or 2 for choices \n";
cout << "[1] ADD AN EMPLOYEE \n";
cout << "[2] LIST ALL EMPLOYEE \n";
cout << "[3] DELETE AN EMPLOYEE \n";
cout << "[4] SEARCH AN EMPLOYEE \n";
cout << "your answer: ";
cin >> a;

switch (a) {
case 1:
fp = fopen("list.txt", "a+");
if (!fp)
{
cout << "Cannot open file. \n";
exit(1);
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "enter first name: \n";
fgets(e.fname, 255, stdin);
cout << "enter last name: \n";
fgets(e.lname, 255, stdin);
cout << "enter employee number: \n";
fgets(e.eno, 255, stdin);
cout << "enter Salary name: \n";
fgets(e.salary, 255, stdin);
fputs("FIRST NAME: ", fp);
fputs(e.fname, fp);
fputs("LAST NAME: ", fp);
fputs(e.lname, fp);
fputs("EMPLOYEE NUMBER:", fp);
fputs(e.eno, fp);
fputs("SALARY AMOUNT: ", fp);
fputs(e.salary, fp);
fclose(fp);
break;
case 2:
fp = fopen("list.txt", "r");
char c;
while ((c = fgetc(fp)) != EOF)
cout << c;
fclose(fp);
break;
case 3:
// for deletion
break;
case 4:
// for search
fp = fopen("list.txt", "r+");
if (!fp)
{
cout << "Cannot open file. \n";
exit(1);
}
cout << "Enter Employee #";
fgets(e.eno, 255, stdin);
fclose(fp);
break;

default:
cout << "try again.";
break;
}
system("pause>0");
return 0;
}
Topic archived. No new replies allowed.