I use VC++ 6.0

1) This small program simply reads infomation from file “in.txt” and write it in file “out.txt”. (copy contents from this file to another)

2) It doesn’t work. The compiler issues an execution-time error. And the ouput text file “out.txt” is empty.

3) I don’t know why it issues an error. Please help me fix it using member function “read”. Thank you handsome guys ^^.

4) My input text file “in.txt” has form:

1 Bob 100
2 Lina 300
3 John -99

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::ostream;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <string>
using std::string;

class ClientData
{
	public:
		ClientData(int acc = 0, string n = "", double bal = 0);
		~ClientData(){};
		void setClientData(int acc, string n, double bal);
		void setAccount(int acc);
		void setName(string n);	
		void setBalance(double bal);
		int getAccount() const {	return account;	}
		double getBalance() const {	return balance;	}
		string getName() const {	return name;	}

	private:
		int account;
		string name;		
		double balance;
};

ClientData::ClientData(int acc, string n, double bal)
{
	setClientData(acc, n, bal);
}

void ClientData::setClientData(int acc, string n, double bal)
{
	setAccount(acc);
	setName(n);	
	setBalance(bal);
}

void ClientData::setAccount(int acc)
{
	account = acc;
}

void ClientData::setName(string n)
{
	name = n.length == 0 ? "No name" : n;
}

void ClientData::setBalance(double bal)
{
	balance = bal;
}

void outputLine(ostream &output, const ClientData &record);

void main()
{
	ifstream in("in.txt");
	ofstream out("out.txt");
	ClientData client;


	// THIS BLOCK CAUSES ERROR //	
	while(in.read(reinterpret_cast<char *>(&client), sizeof(ClientData)))
	{
		outputLine(out, client);
	}	
}

void outputLine(ostream &output, const ClientData &record)
{		
	output << record.getAccount() << " ";
	output << record.getName() << " ";
	output << record.getBalance() << " ";
	output << endl;
}

you can't use read() because:
in text file "1 Bob 100" = string
so 1 = char, but your class need int
and ' ' = char too, but your class dont need it

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::ostream;
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <string>
using std::string;

class ClientData
{
public:
	ClientData(int acc = 0, string n = "", double bal = 0);
	~ClientData()
	{
	}
	;
	void setClientData(int acc, string n, double bal);
	void setAccount(int acc);
	void setName(string n);
	void setBalance(double bal);
	int getAccount() const
	{
		return account;
	}
	double getBalance() const
	{
		return balance;
	}
	string getName() const
	{
		return name;
	}

private:
	int account;
	string name;
	double balance;
};

ClientData::ClientData(int acc, string n, double bal)
{
	setClientData(acc, n, bal);
}

void ClientData::setClientData(int acc, string n, double bal)
{
	setAccount(acc);
	setName(n);
	setBalance(bal);
}

void ClientData::setAccount(int acc)
{
	account = acc;
}

void ClientData::setName(string n)
{
	name = n.length() == 0 ? "No name" : n;
}

void ClientData::setBalance(double bal)
{
	balance = bal;
}

void outputLine(ostream &output, const ClientData &record);

int main()
{
	ifstream in("in.txt");
	ofstream out("out.txt");

	int account;
	string name;
	double balance;
	while (in >> account >> name >> balance)
	{
		ClientData client(account, name, balance);
		outputLine(out, client);
	}
	return 0;
}

void outputLine(ostream &output, const ClientData &record)
{
	output << record.getAccount() << " ";
	output << record.getName() << " ";
	output << record.getBalance() << " ";
	output << endl;
}

1) Excuse me, your method you give me is "sequential access". I'd like to use "random access" with member function 'read' and 'write'. That's the only way to do this exercise.

2) I tried to finish the following code all time today (i'm really tired) but it doesn't work T.T. Function addAccount and function print have problems >.<.

3) Could you please fix it for me(give me a fish ^^)? Thank you handsome guy.

This is my unfinished program, not small like above.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::ostream; ////////////// PROJECT ////////////
#include <fstream>
using std::fstream;
using std::ifstream;
using std::ofstream;
#include <string>
using std::string;

class ClientData
{
	public:

	ClientData(int acc = 0, string n = "", double bal = 0);
	~ClientData(){};
	void setClientData(int acc, string n, double bal);
	void setAccount(int acc);
	void setName(string n);	
	void setBalance(double bal);
	int getAccount() const {	return account;	}
	double getBalance() const {	return balance;	}
	string getName() const {	return name;	}

	private:
		int account;
		string name;		
		double balance;
};

ClientData::ClientData(int acc, string n, double bal)
{
	setClientData(acc, n, bal);
}

void ClientData::setClientData(int acc, string n, double bal)
{
	setAccount(acc);
	setName(n);	
	setBalance(bal);
}

void ClientData::setAccount(int acc)
{
	account = acc;
}

void ClientData::setName(string n)
{
	name = n;
}

void ClientData::setBalance(double bal)
{
	balance = bal;
}

int enterChoice();
int getAccount();
void addAccount(fstream &storeFile);
void print(fstream &storeFile);
void outputLine(ostream &output, const ClientData &record);
enum CHOICES{ADD = 1, DEL, PRINT, END};

int main()
{
	int choice;
	fstream storeFile("store.dat");

	do
	{
		choice = enterChoice();
		
		switch(choice)
		{
			case ADD: 
				addAccount(storeFile);
				break;
			case DEL:
				//deleteAccount();
				break;
			case PRINT:
				print(storeFile);
				break;
			case END:
				cout << "End" << endl;
				break;
			default:
				cout << "Incorrect." << endl;
				break;
		}
		storeFile.clear();		
	}
	while(choice != 4);

	return 0;
}

int enterChoice()
{
	int ch;
	cout << "1. Add a new account" << endl;
	cout << "2. Delete a account " << endl;
	cout << "3. Print text file  " << endl;
	cout << "4. End" << endl;
	cout << "\tEnter your choice: ";
	cin >> ch;
	return ch;
}

// create and insert record
void addAccount(fstream &storeFile)
{
	cout << "=========== Add new account =============" << endl;	
	
	ClientData client;	
	int recordNumber = getAccount();	
	
	storeFile.seekg((recordNumber - 1) * sizeof(ClientData));
	storeFile.read(reinterpret_cast<char *>(&client), sizeof(ClientData));

	// create record, if record does not previously exist
	if(client.getAccount() == 0)
	{
		string name;
		double balance;

		cout << "Enter name: ";
		cin >> name; client.setName(name);
		cout << "Enter balance: ";
		cin >> balance; client.setBalance(balance);
		client.setAccount(recordNumber);		
		
		// move file-position pointer to correct record in file 
		storeFile.seekp((client.getAccount() - 1) * sizeof(ClientData));

		// insert record in file 
		storeFile.write(reinterpret_cast<const char*>(&client), sizeof(ClientData));

		cout << "Finish adding" << endl;
	}
	else
		cout << "This item HAD information" << endl;
}

// create formatted text file for printing
void print(fstream &storeFile)
{
	cout << "=========== Print text file =============" << endl;		
	
	ofstream printFile("print.txt");
	
	ClientData client;

	storeFile.read(reinterpret_cast<char *>(&client), sizeof(ClientData));
	while(!storeFile.eof() || !storeFile)
	{
		if(client.getAccount() != 0)
			outputLine(printFile, client);
		storeFile.read(reinterpret_cast<char *>(&client), sizeof(ClientData));
	}
}

void outputLine(ostream &output, const ClientData &record)
{		
	output << record.getAccount() << " ";
	output << record.getName() << " ";
	output << record.getBalance() << " ";
	output << endl;
}

int getAccount()
{
	int account;
	cout << "Enter account number: ";
	cin >> account;
	return account;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

class ClientData
{
public:

	ClientData(int _acc = 0, string n = "", double bal = 0);
	~ClientData()
	{
	}
	;
	void setClientData(int _acc, string n, double bal);
	void setAccount(int _acc);
	void setName(string n);
	void setBalance(double bal);
	int getAccount() const
	{
		return account;
	}
	double getBalance() const
	{
		return balance;
	}
	string getName() const
	{
		return name;
	}
private:
	int account;
	char name[50];
	double balance;
};

ClientData::ClientData(int _acc, string n, double bal)
{
	setClientData(_acc, n, bal);
}

void ClientData::setClientData(int _acc, string n, double bal)
{
	setAccount(_acc);
	setName(n);
	setBalance(bal);
}

void ClientData::setAccount(int _acc)
{
	account = _acc;
}

void ClientData::setName(string n)
{
	strcpy(name, n.c_str());
}

void ClientData::setBalance(double bal)
{
	balance = bal;
}

int enterChoice();
int getAccount();
void addAccount(string &storeFile);
void print(string &storeFile);
void outputLine(const ClientData &record);
enum CHOICES
{
	ADD = 1, DEL, PRINT, END
};

int main()
{
	int choice;
	string storeFile = "store.dat";
	do
	{
		choice = enterChoice();

		switch (choice)
		{
		case ADD:
			addAccount(storeFile);
			break;
		case DEL:
			//deleteAccount();
			break;
		case PRINT:
			print(storeFile);
			break;
		case END:
			cout << "End" << endl;
			break;
		default:
			cout << "Incorrect." << endl;
			break;
		}
	} while (choice != 4);

	return 0;
}

int enterChoice()
{
	int ch;
	cout << "1. Add a new account" << endl;
	cout << "2. Delete a account " << endl;
	cout << "3. Print text file  " << endl;
	cout << "4. End" << endl;
	cout << "\tEnter your choice: ";
	cin >> ch;
	return ch;
}

// create and insert record
void addAccount(string &storeFile)
{
	ofstream file;
	file.open(storeFile.c_str(), ios::app);
	if (file.is_open())
	{
		cout << "=========== Add new account =============" << endl;

		int recordNumber = getAccount();
		string name;
		double balance;

		cout << "Enter name: ";
		cin >> name;

		cout << "Enter balance: ";
		cin >> balance;

		ClientData client(recordNumber, name, balance);

		file.write(reinterpret_cast<const char*> (&client), sizeof(ClientData));

		cout << "Finish adding" << endl;
		file.close();
	}
	else
	{
		cout << "Unable to open file " << storeFile << endl;
	}

}

// create formatted text file for printing
void print(string &storeFile)
{
	ifstream ifile;
	ifile.open(storeFile.c_str());
	if (ifile.is_open())
	{
		cout << "=========== Print text file =============" << endl;
		ClientData client;
		while (ifile.read(reinterpret_cast<char *> (&client), sizeof(ClientData)))
		{
			outputLine(client);
		}
		cout << "============ End of file ==============" << endl;
		ifile.close();
	}

	else
	{
		cout << "Unable to open file " << storeFile << endl;
	}
}
void outputLine(const ClientData &record)
{
	cout << record.getAccount() << " ";
	cout << record.getName() << " ";
	cout << record.getBalance() << " ";
	cout << endl;
}

int getAccount()
{
	int account;
	cout << "Enter account number: ";
	cin >> account;
	return account;
}

I'm sorry but It doesn't work as i expect >.<.

1) Function addAccount must check if the input record had information (through account number). If It hasn't had information, the function 'write' executes, else cout << "This record had information".

2) And the member function 'write' must write at the specified record. It means when i enter:

choice: 1
account: 1
name: Bob
balance: 1

choice: 1
account: 4
name: Lina
balance: 3

then the "print.txt" must represents:

1 Bob 1
0 0
0 0
4 Lina 3

By the way, the function print doesn't work too >.<

Thank you for spending your precious time.

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