•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 429,794 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,804 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1288 | Replies: 2
![]() |
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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;
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;
>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.
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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Jul 2005
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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;
}
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the C Forum
- Previous Thread: Conserving resources and scrolling in files
- Next Thread: IMF message filter SDK sample problem



Linear Mode