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;
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];
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);
};
// 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;
//***************************************************************
// global declaration for stream object, object
//****************************************************************
//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:";
//initialization of the static function
int client::count;
//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************
//***************************************************************
// 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();
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
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
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.