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;

Recommended Answers

All 2 Replies

>I need help.
Yes. Yes, you do.

>here's my confusing code...
That's an understatement. Not only did you neglect to use code tags, which killed any formatting your code may have had, the only comments you use are utterly useless. Perhaps instead of just saying "here's my confusing code", you could give us the code, tell us what you want it to do, tell us what is isn't doing that you want it to do, and provide an example input file along with the expected output. If you fail to do this, I'll be forced to treat you like an idiot and link you here.

sorry about that, its my first time here.
ok..
what im trying to do is to read from the input file and pass it into a structure.

void open_file( ifstream& master, ifstream& transact )
{
	// attempts to open the master file and transaction file
	string master;
	string 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 read_file( ifstream& master, ifstream& transact )
{
	char ssn[9], transact_ssn[9],
		 name[20],
		 street[20],
		 city[20],
		 state[2],
		 zip[5],
		 dummy[2];
	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 >> transact_ssn;
	while( !transact.eof() )
	{
		transact >> transact_ssn;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.