Someone help me please!

My teacher is no help. how do I write to and read the file? MUST BE IN BINARY

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define FILENAME "S:\\Unit_5_text_document.txt"
int x;
int choice=0;
char* comment;

int main(int argc, char* argv[]) {

fstream fin(FILENAME, ios::in | ios::binary);

while (choice!=5) {
cout << "what would you like to do?" << endl;
cout
<< "1.Read - 2.Write - 3.Size of file - 4.Number of characters in the file - 5.End"
<< endl;
cin>> choice;

if (choice==1) {
string buffer;

while (!fin.eof()) {

fin.read((char *) &x, sizeof(x));

if (fin.fail() && !fin.eof()) {

cerr << "Error reading " << FILENAME << "." << endl;
return 1;

}

cout << buffer << endl;

}
}
if (choice==2) {
cout << "enter the text you want to add" << endl;
cin >> comment;
fin.write((char *) &comment, sizeof(comment));
}
if (choice==3) {
streampos begin, end;
ifstream myfile(FILENAME, ios::binary);
begin = myfile.tellg();
myfile.seekg(0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
if (choice==4) {

}
if (choice>5) {
cerr << "Invalid Option" <<endl;
}

}

}
I HOPE ONCE YOU COMPILE THESE LINES THEY WILL BE VERY USEFUL TO YOU SINCE IT
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <process.h>
#include <iomanip>
#include <cstring>
//***********************************************
// GLOBAL VALUES
//***********************************************
float weekly_bill=5000;

struct contact{
char street[30];
char address[30];
char telephone_number[40];
};
//END OF GLOBAL VALUES

class client{
private:

protected:
static int count;
char client_name[30];

contact client_contact;
public:
char client_id[10];
static int return_count(){
++count ;
return count;
}
char* return_client_id(){
return client_id;
}
friend void write(client &);
friend void display_all_clients(client &) ;
//friend void search_client_info(client &,char n[]);

virtual void create_client_info(){
return_count();
cout<<" No: "<<count;
cout<<"\n Enter the Client's ID: ";gets(client_id);
cout<<"\n Enter the Client's Name: ";gets(client_name);
cout<<"\nEnter the Client's Street: ";gets(client_contact.street);
cout<<"\nEnter the Client's Address: ";gets(client_contact.address);
cout<<"\nEnter the Client's Telephone: ";gets(client_contact.telephone_number);

}
void display_client_info(){
cout<<"\n Client ID: "<<puts(client_id);

cout<<"\n Client's Name : ";
puts(client_name);
cout<<"\n Client's Street : ";
puts(client_contact.street);
cout<<"\n Client's Address : ";
puts(client_contact.address);
cout<<"\n Client's Telephone:";puts(client_contact.telephone_number);

}

void report()
{cout<<"\n "<<client_id<<setw(10)<<" "<<client_name<<setw(10)<<" "<<client_contact.street<<setw(10)<<" "<<client_contact.address<<setw(10)<<" "<<client_contact.telephone_number<<setw(10)<<endl;}


};
// end of abstract base class client
//BEGINNING OF VIP DERIVED CLASS
class VIP_client:public client{
private:
float bill;
public:
float get_vip_monthly_bill(){
bill=(weekly_bill*4);
return bill;
}
void create_client_info(){
client::create_client_info();
get_vip_monthly_bill();
cout<<"VIP's monthly bill payment: "<<bill;

}



/*void display_client_info(){
cout<<"\n VIP";
client::display_client_info();

} */

};//end of VIP class

//beginning of regular class

class regular_client:public client{
private:
float bill;
public:
float get_monthly_bill(){
bill=(weekly_bill*2);
return bill;
}
void create_client_info(){
client::create_client_info();
get_monthly_bill();
cout<<"REGULAR's monthly bill payment: "<<bill;

}



/* void display_client_info(){
cout<<"\n REGULAR";
client::display_client_info();

} */
};

//***************************************************************
// global declaration for stream object, object
//****************************************************************

fstream fp,fp1;
client general;
client *monthlybill;
VIP_client VIP;
regular_client regular;


//WRITING ON THE CLIENT RECORDS
void write(client &type){
char ch;
fp.open("clients.dat",ios::out|ios::app);
do
{
clrscr();
type.create_client_info();
fp.write((char*)&type,sizeof(client));
cout<<"\n\nDo you want to add more record..(y/n?)";
cin>>ch;
}while(ch=='y'||ch=='Y');
fp.close();
}
//END WRITING

//READING FROM CLIENT INFO
void search_client_info(client &type,char n[]){
cout<<"\CLIENT DETAILS\n";
int flag=0;
fp.open("clients.dat",ios::binary|ios::in);
while(fp.read((char*)&type,sizeof(client)))
{
if(strcmpi(type.return_client_id(),n)==0)
{
type.display_client_info();
flag=1;
getch();
}
}

fp.close();
if(flag==0)
cout<<"\n\n Client does not exist";
getch();

}
void display_all_clients(client &type)
{
clrscr();
fp.open("clients.dat",ios::in);
if(!fp)
{
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch();
return;
}

cout<<"\n\n\t\tCLIENT LIST\n\n";
cout<<"Client ID: "<<" Client's Name : "<<" Client's Street : "<<" Client's Address : "<<"Telephone:";

while(fp.read((char*)&type,sizeof(client)))
{
type.report();
}
fp.close();
getch();
}


//DELETE THE VIP CLIENT
void delete_client(client &type)
{
char n[6];
clrscr();
cout<<"\n\n\n\tDELETE CLIENT...";
cout<<"\n\nEnter The Client ID of the client you wish to delete : ";
cin>>n;
fp.open("clients.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&type,sizeof(client)))
{
if(strcmpi(type.return_client_id(),n)!=0)
{
fp2.write((char*)&type,sizeof(client));
}
}

fp2.close();
fp.close();
remove("clients.dat");
rename("Temp.dat","clients.dat");
cout<<"\n\n\tRecord Deleted ..";
getch();
}

//initialization of the static function
int client::count;


//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************

void intro()
{
clrscr();
gotoxy(20,11);
cout<<"TICKTAKA";
gotoxy(20,14);
cout<<"CLIENT RECORDS";
gotoxy(20,17);
cout<<"MANAGEMENT SYSTEM";

getch();
}



//***************************************************************
// ADMINISTRATOR MENU FUNCTION
//****************************************************************

void admin_menu()
{
clrscr();
int ch2;
cout<<"\n\n\n\t CHOOSE ONE OF THE FOLLOWING OPTIONS";
cout<<"\n\n\t1.CREATE CLIENT RECORD";
cout<<"\n\n\t2.DISPLAY ALL CLIENS RECORD";
cout<<"\n\n\t3.SEARCH FOR CLIENTS IN RECORD ";

cout<<"\n\n\t4.DELETE CLIENT RECORD";

cout<<"\n\n\t5.EXIT";
cout<<"\n\n\tPlease Enter Your Choice (1-5) ";
cin>>ch2;
switch(ch2)
{
case 1: {clrscr();
int ch3;
cout<<"\n\n\n\t CHOOSE ONE OF THE FOLLOWING OPTIONS";
cout<<"\n\n\t1.CREATE VIP CLIENT RECORD";
cout<<"\n\n\t2.CREATE REGULAR CLIENT RECORD";
cout<<"\n\n\t3.RETURN TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (1-3) ";
cin>>ch3;
switch(ch3)
{ case 1:
write(VIP);
break;
case 2:
write(regular);
case 3:
return;
break;
default:
cout<<"\a";


}

}
break;
case 2:
display_all_clients(general);
break;
case 3: {
char num[10];
clrscr();
cout<<"\n\n\tPlease Enter The Client's ID. ";
gets(num);
search_client_info(VIP,num);
break;
};

case 4:{
delete_client(VIP);
break;
}


case 5:
exit(0);

break;
default:cout<<"\a";
}
admin_menu();
}


int main(){
char ch;
intro();
do
{
clrscr();
admin_menu();

}while(ch!='4');

getch();
return 0;


}
As you can observe to save files in binary mode, they should end with .bat and not .txt

The above codes will help you in
object oriented approach in c++
how to read files
how to write on files
how to search on files
and how to delete files
http://www.cplusplus.com/doc/tutorial/files/
Search that page for the word "binary", and you're good to go.
Thanks for the replies, I got reading and writing working now. can someone help with counting the characters in the file, I have no idea where to start
JESUS CHRIST! PLEASE USE CODE TAGS!!!
Note that chcsum's suggestion to use .BAT as the file extension is totally wrong in the Windows world; that extension is reserved for batch files, which are text files containing batch commands.

For generic data you can use .DAT, but many for binary data file formats have different extensions. So you could give your file an app specific extension.

See the following list for various examples:
http://www.file-extensions.org/filetype/extension/name/binary-files

Andy
Last edited on
can someone help with counting the characters

You said you're file was binary, so not sure how characters fit in.

If encoded using ASCII each char (or a text file) is a single byte, if that helps.

Andy
Topic archived. No new replies allowed.