RSS Forums RSS
Please support our C advertiser: Programming Forums

I need help on STRUCTURES and on input file

Join Date: Jul 2005
Posts: 2
Reputation: truxpinoyxrl17 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
truxpinoyxrl17 truxpinoyxrl17 is offline Offline
Newbie Poster

Solution I need help on STRUCTURES and on input file

  #1  
Jul 27th, 2005
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;
AddThis Social Bookmark Button
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:56 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC