I need help. I need help on reading input file using .READ and normal streams. I also need help on sorting input file and passing data to the structure rec and output it without showing any null characters.....
here's my confusing code...
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#define MASTER "F:\\Program Assignment 6\\master.txt"
#define TRANSACT "F:\\Program Assignment 6\\transact.txt"
struct rec
{
char ssn[9],
name[20],
street[20],
city[20],
state[2],
zip[5],
dummy[2];
};
void OpenFile(ifstream&, ifstream&);
//void ReadFile(ifstream&, ifstream&);
void SortFile(int [], int);
void swap(int&, int&);
void main()
{
rec record;
ifstream master;
ifstream transact;
cout << "Address.\n\n";
OpenFile(master, transact);
SortFile(transact_ssn, 9);
master.read((char *) &record, sizeof(record));
while (!master.eof())
{
cout << record.ssn << record.dummy
<< record.name << record.dummy
<< record.street << record.dummy
<< record.city << ", "
<< record.state<< ' '
<< record.zip << record.dummy
<< "\n\n\n" << record.dummy;
master.read((char *) &record, sizeof(record));
}
while (!transact.eof())
{
master.read((char *) &record, sizeof(record));
}
cout << endl;
master.close();
}
void OpenFile( ifstream& master, ifstream& transact )
{
master.open( MASTER, ios::nocreate | ios::binary );
transact.open( TRANSACT, ios::nocreate);
if(!master)
cout << "** Can't open " << MASTER << " **" << endl;
if(!transact)
cout << "** Can't open " << TRANSACT << " **" << endl;
}
void ReadFile( ifstream& master, ifstream& transact )
{
char ssn[], transact_ssn[],
name[],
street[],
city[],
state[],
zip[],
dummy[];
rec record;
master.read((char *) &record, sizeof(record));
while( !master.eof() )
{
master.read(ssn, 9);
master.read(name, 20);
master.read(street, 20);
master.read(city, 20);
master.read(state, 2);
master.read(zip, 5);
master.read(dummy, 2);
master.read((char *) &record, sizeof(record));
}
transact.read((char *) &record, sizeof(record));
while( !transact.eof() )
{
transact >> transact_ssn;
transact.read((char *) &record, sizeof(record));
}
}
void PrintFile()
{
rec record;
cout << record.ssn << record.dummy
<< record.name << record.dummy
<< record.street << record.dummy
<< record.city << ", "
<< record.state<< ' '
<< record.zip << record.dummy
<< "\n\n\n" << record.dummy;
void SortFile( int file[], int size )
{
int pass; // Number of pass.
int i; // Index variable.
// Do size - 1 passes.
for (pass = 1; pass < size; ++pass)
// On each pass, compare size - pass pairs of elements.
for (i = 0; i < size - pass; ++i)
if (file[i] > file[i + 1])
swap(file[i], file[i + 1]); // Swap if out of ascending order
}
void swap(int& a, int& b)
{
int temp;
temp = a;
a = b;