Hi! my dear all.

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class payroll
{
private:
char address[50];
float basic,allowence,deduction;
public:
void add(void);
};
void payroll::add(void)
{
clrscr();
int e_no,choice;
char name[50],ch;
payroll e;
cout<<"Enter Employe Number: ";cin>>e_no;
cout<<"Enter Employe Name: ";gets(name);
cout<<"Enter Employe's Address: ";gets(e.address);
cout<<"Enter Employe's Basic Salary: ";cin>>e.basic;
cout<<"Enter Employe's Allownce: ";cin>>e.allowence;
cout<<"Enter Deduction: ";cin>>e.deduction;
cout<<"\n\dfn1-> Save    2-> Cancel";cin>>choice;
if(choice==1)
{
FILE*fptr;
fptr=fopen("d:\\bank.dat","r+");
fwrite(&e,sizeof(e),1,fptr);
fclose(fptr);
}
else if(choice==2)
cout<<"Record Cancelled";
clrscr();
}
payroll emp;
void main(void)
{
clrscr();
char ch;
int choice;
cout<<"\n============main menu============";
cout<<"\n1 -> Add Record";
cout<<"\n2 -> List Record";
cout<<"\n3 -> Delete Record";
cout<<"\n4 -> Update Record";
cout<<"\nq -> Quit\n\n";
cin>>ch;
switch(ch)
{
case'1': emp.add();break;
case'q': break;
}
getch();
}

Here is my code.
I am trying to create a payroll of an employee. As you can see by code that I am yet able to create record just.
But after giving all information when I try to save my record by pressing 1, nothing happens and when I press 2 it should show a statement "Record canceled" but it is not.

Edited 5 Years Ago by Nick Evan: Added [CODE] tags, next time add them yourself.

I'm gonna take a wild guess and say that you're trying to write the entire class object (not a buffer) to a file.
Apart from that, you should read up on C++ reference: fwrite, I think you misplaced the two parameters for size and count.

If you want to write your class object to a file, you'll have to put all your variables from your class object in a buffer, much like the C++ reference example.

Edited 5 Years Ago by ShadowScripter: n/a

So please can you tell me that where should I edit in my code as you saying to write buffer to a file.
Please help me as I am using first time I/O system(file system).

i would say that is a very poor code. you're missing the indentations, and the code is not generic as it doesn't follow the conventions. its hard to understand for any other programmer.
you can IO using classes and objects as in your case. follow this code and you'll get the idea of IO using objects. But the file created this way is binary.

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

class CRecordManager
{
private:
	int m_num ;
	char m_name [30] ;
public:
	void fnRecord ( void ) ;
	void fnDisplay( void ) ;
};

void CRecordManager :: fnRecord ( void )
{
	cout << "EMPNUMBER: " ;		cin >> m_num ;
	cout << "EMPNAME: " ;	cin >> m_name ;
}
void CRecordManager :: fnDisplay( void )
{
	cout << "Employee Number: " << m_num << " and " << "Employee Name: " << m_name << endl ;
}

int main()
{
	CRecordManager oEmp01 ;
	oEmp01 . fnRecord() ;
	ofstream oWrite ;		// Object to write to file
	oWrite.open ( "Record.txt") ;		// details stores in this file
	oWrite.write( (char * )  & oEmp01 , sizeof ( oEmp01 ) ) ;		//Writing to file
	oWrite.close() ;

	cout << endl << endl ;

	ifstream oRead ;		// obj to read frm file
	oRead . open ( "Record.txt" ) ;
	oRead.read ( (char * )  & oEmp01 , sizeof ( oEmp01 ) ) ;		// reading
	oEmp01 . fnDisplay () ;	// display the contents read
	oRead.close() ;

	system ("pause") ;
	return 0 ;
}

But I am using Turbo C++ IDE I am not using object oriented but simple C++. So therefore I included header files with extension(.h).
And where will the record.txt will be saved? You did not given any path to the file.

Edited 5 Years Ago by Shayaan_Mustafa: n/a

i didn't mean that using .h files makes your program non standard. it has a lot of other issues too. you have included too many library files, and haven't used them.
main is not returning any value.
identifiers are not following conventions.
no comments are used to make any other programmer aware of whats happening in the program.
also,
record.txt file is stored in the same place your exe file is stored. you can also provide a full path in the codes, like, "c:\RecordManager\Database\record.txt" etc. I just presented an example.

Ok thanks a lot. Now if I want to create a text file i.e. .txt so what should I do? I don't want binary file.
Please help me.

the file here would be .txt, but the content would be binary. you can for sure open the file, but you cannot read it, unless you can understand binary.
if you want the contents in simple english, you cannot use read() and write() functions as these work only with binary file.
lookup file handling here, or just google it for other results.

This article has been dead for over six months. Start a new discussion instead.