using fstream

Create a class Employee with name, Id and salary as data members. Provide appropriate
constructors, set, get and display methods in the class.
In the main program, use a do-while loop to enter data for employees as long as the user
desires and save all data to a file.
Once the user is done with data entry, read the data for employees from the file and
display the information of each employee.
This is how i got so far but it does not write in file and so cant read the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  class Employee
{
private:
    char name[20];
	char id[20];
	int salary;
public:
	Employee()
	{
	strcpy(name,"none");
	strcpy(id,"none");
	salary=0;
	}
	void setall(char *n , char *i, int s)
	{
		strcpy(name,n);
		strcpy(id,i);
		salary=s;
	}
	void display()
	{
		cout<<"name : "<<name<<endl;
		cout<<"Id  : "<<id<<endl;
		cout<<"salary : "<<salary<<endl;
	}
};

int main()
{
	char choice;
	char na[20], I[20];
	int sal;
	Employee e;
	fstream file;
	file.open("Employee.txt",ios::app|ios::binary);
	do
	{ 
		cout<<"ENter name\n";
		file>>na;
		cin>>na;
		
		cout<<"Enter id \n";
		file>>I;
		cin>>I;
		cout<<"Enter Salary\n";
		file>>sal;
		cin>>sal;
		file.write(reinterpret_cast<char *>(&e),sizeof(e));
		e.setall(na,I,sal);
		cout<<"Do you want to add another entry y for yes\n";
		cin>>choice;
	}
	while(choice=='y' || choice=='Y');
	/*file.seekg(0);*/
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	while(!file.eof())
	{
	e.display();
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	}
	cout<<endl;
	getch();
}
	
Are you sure your file is properly opening? You should always check and insure the file opened correctly.

Why are you mixing the extraction operator and the write() function. You should probably only be using the write() function since you're using a binary file. You should also flush() the output before you try to read the file.

Is there an actual reason you're trying to use a binary file format instead of a plain text file format?
All those different methods of insertion , binary are applied because the normal method weren't working
Look at this snippet:
1
2
3
		cout<<"ENter name\n";
		file>>na;
		cin>>na;

Why are you trying to write a value to the file before you actually retrieve the value from the user? But really you shouldn't be writing to the file here at all, just use the proper write() function call.

Again look at this snippet:
1
2
		file.write(reinterpret_cast<char *>(&e),sizeof(e));
		e.setall(na,I,sal);

Why are you trying to write the class to your file before you assign values to the variables?

You don't seem to realize that statements are executed from the top down in a C/C++ program.

Again why are you trying to use the much more complicated binary file write modes? I really suggest you first use plain text mode until you actually understand C++ file input and output. With plain text mode you can use a text editor to actually see that the information is being properly written. With binary mode you need a hex editor to see that the information is actually be properly written.
file>>na; This is trying to read from the file. But given that on the very next line, the cin>>na; will modify the value of na, it doesn't make any kind of sense.

As a general comment, check the file status to verify that the program is working as expected. After opening the file, check it is open. After writing to the file, check that the write succeeded etc.
1
2
3
4
    if (!file)
    {
        cout << "File error 1 " << endl;
    }

Use a different number or different text each time so that you will know which part of the program is producing the message.

And perhaps change the file open mode to specify input and output:
file.open("Employee.bin",ios::in|ios::out|ios::app|ios::binary);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Employee
{
private:
    char name[20];
	char id[20];
	int salary;
public:
	Employee()
	{
	strcpy(name,"none");
	strcpy(id,"none");
	salary=0;
	}
	void setall(char *n , char *i, int s)
	{
		strcpy(name,n);
		strcpy(id,i);
		salary=s;
	}
	void display()
	{
		cout<<"name : "<<name<<endl;
		cout<<"Id  : "<<id<<endl;
		cout<<"salary : "<<salary<<endl;
	}
};

int main()
{
	char choice;
	char na[20], I[20];
	int sal;
	Employee e;
	fstream file;
	file.open("Employee.txt",ios::in|ios::out|ios::app|ios::binary);
	do
	{ 
		cout<<"ENter name\n";
		cin>>na;
		
		cout<<"Enter id \n";
		cin>>I;
		cout<<"Enter Salary\n";
		cin>>sal;
		e.setall(na,I,sal);
		file.write(reinterpret_cast<char *>(&e),sizeof(e));
		cout<<"Do you want to add another entry y for yes\n";
		cin>>choice;
	}
	while(choice=='y' || choice=='Y');
	/*file.seekg(0);*/
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	while(!file.eof())
	{
	e.display();
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	}
	cout<<endl;
	getch();
}

I made changes in this code still wont give correct output in console
What about line 51 (currently commented out with /* */). Maybe reinstate that line
Since you want to read from the beginning of the file you must first seek to the beginning. Try uncommenting the file.seekg().
Yes changing that does show output is shows previous output too i want to see only the current output removing ios::app does change the output but still does not give only the current output only
I find text writing confusing to implement this thing for example what if
write a program which allows
user to enter the name of an Employee whose record is searched in the data file. Once
found, display the information of the concerned employee. If the record is not found,
display an error message.
There is a problem in this code can we use string in fstream? anyway here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<conio.h>
#include<iostream>
#include<fstream>
using namespace std;
class Employee
{
private:
    char name[20];
	char id[20];
	int salary;
public:
	Employee()
	{
	strcpy(name,"none");
	strcpy(id,"none");
	salary=0;
	}
	void setall(char *n , char *i, int s)
	{
		strcpy(name,n);
		strcpy(id,i);
		salary=s;
	}
	void display()
	{
		cout<<"name : "<<name<<endl;
		cout<<"Id  : "<<id<<endl;
		cout<<"salary : "<<salary<<endl;
	}
	char getn()
	{
		return * name;
	}
};

int main()
{
	char choice;
	char na[20], I[20];
	int sal;
	Employee e;
	fstream file;
	file.open("Employee.txt",ios::in|ios::out|ios::binary);
	do
	{ 
		cout<<"ENter name\n";
		cin>>na;
		
		cout<<"Enter id \n";
		cin>>I;
		cout<<"Enter Salary\n";
		cin>>sal;
		e.setall(na,I,sal);
		file.write(reinterpret_cast<char *>(&e),sizeof(e));
		cout<<"Do you want to add another entry y for yes\n";
		cin>>choice;
	}
	while(choice=='y' || choice=='Y');
	file.seekg(0);
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	while(!file.eof())
	{
	e.display();
	file.read(reinterpret_cast<char *>(&e),sizeof(e));
	}
	cout<<endl;
	cout<<"Enter name to search the employee record\n";
	cin>>na;
	file.seekg(0);
	while(!file.eof())
	{
		if(strcmp(na,e.getn())==0)
		{
			e.display();
		}
		else 
			cout<<"no record found\n";
	}
	getch();
}
Making name public does remove error but still does not display
One of your problems is that getn() is returning a single character, not a string. Also returning a pointer without the proper const qualifier is a bad practice, because the pointer gives you access to that private variable outside of the class.
Yes but doing that it still does not solve the main problem
and how can i return the whole array instead of single character i did put const though
Topic archived. No new replies allowed.