RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1335 | Replies: 2
Reply
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  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: I need help on STRUCTURES and on input file

  #2  
Jul 27th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote  
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

Re: I need help on STRUCTURES and on input file

  #3  
Jul 27th, 2005
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;
	}
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:09 am.
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