PDA

View Full Version : Help with Binary files and Structures


Zoer
04-26-2003, 11:24 PM
Hi

Any help anyone could give here would be greatly appreciated. I am to write two programs, one that writes 3 structures to a binary file, which I have done and is fine as far as I can tell, and the second one that reads the binary file and outputs the structures. It appears to work in the beginning but then it halts with an access violation and I can't fix it!!!!

Here is the code for the first program that creates the binary file:


#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>


struct issue{
char recordi[2];
char customeri[6];
char part[7];
char issamt[5];
}issu;

struct despatch{
char recordd[2];
char customerd[6];
}despat;

struct customer{
char recordc[2];
char customerc[6];
char name[21];
char address[61];
char balance[10];
char credit[8];
}custom;



char in_string[256];


bool(record1(char reci));
bool(record2(char recd));
bool(record3(char recc));
bool(checkdigits(const char* code));

int main(int argc, char* argv[])
{


fstream input_file,output_file, prnfile;

output_file.open("c:\\Bin.bin",ios::out|ios::binary);
if(!output_file.is_open())
{
cout << "The Binary File could not be created" << endl;
exit(1);
}

input_file.open("c:\\514664td.txt", ios::in|ios::nocreate);

if(input_file.fail())
{
cout <<"file does not exist" << endl;
exit(1);
}
prnfile.open("lpt1", ios::out);
input_file.seekg(0L, ios::beg);


while(!input_file.eof())
{
input_file.getline(in_string, 256);

if(record1(in_string[0]))
{
strncpy(issu.recordi, in_string,1);
issu.recordi[1] = NULL;
strncpy(issu.customeri, in_string+1, 5);
issu.customeri[5] = NULL;
strncpy(issu.part, in_string+6, 6);
issu.part[6] = NULL;
strncpy(issu.issamt, in_string+12, 4);
issu.issamt[4] = NULL;

if((checkdigits(issu.customeri)) && (checkdigits(issu.part)))

output_file.write((char*)&issu, sizeof(struct issue));

else
cout << "The Record: " << issu.recordi << " " << issu.customeri
<< " " << issu.part<< " " << issu.issamt
<< " Is an Invalid issu record" << endl << endl;

}
else if (record2(in_string[0]))
{
strncpy(despat.recordd, in_string, 1);
despat.recordd[1] = NULL;
strncpy(despat.customerd, in_string+1, 5);
despat.customerd[5] = NULL;

if(checkdigits(despat.customerd))

output_file.write((char*)&despat, sizeof(struct despatch));



else
cout << "The Record: " << despat.recordd << " " << despat.customerd
<< " Is an Invalid Despatch record" << endl << endl;
}

else if (record3(in_string[0]))

{
strncpy(custom.recordc, in_string, 1);
custom.recordc[1] = NULL;
strncpy(custom.customerc, in_string+1, 5);
custom.customerc[5] = NULL;
strncpy(custom.name, in_string+6, 20);
custom.name[20] = NULL;
strncpy(custom.address, in_string+26, 60);
custom.address[60] = NULL;
strncpy(custom.balance, in_string+86, 9);
custom.balance[9] = NULL;
strncpy(custom.credit, in_string+95, 7);
custom.credit[7] = NULL;
if(checkdigits(custom.customerc))
{
output_file.write((char*)&custom, sizeof(struct customer));
}
else
cout << "The Record: " << custom.recordc << " " << custom.customerc
<< " " <<< " " << custom.credit << endl << endl;
}
else
cout << "The Record :"<< in_string << " Is an invalid record type" << endl << endl;
}

output_file.close();

return 0;
}

bool(record1( char reci)) //Function to Validate Record Code I or R
{
switch(in_string[0])
{
case 'i':
case 'I':
case 'r':
case 'R': return true;
break;
default: return false;
}
}
bool(record2(char recd)) //Function to validate Record Code D
{
switch(in_string[0])
{
case 'd':
case 'D': return true;
break;
default: return false; //End of record Function
}
}

bool(record3(char recc)) //Function to validate Customer Record C
{
switch(in_string[0])
{
case 'c':
case 'C': return true;
break;
default: return false;
}
}

bool(checkdigits(const char* code)) // Check Digit Function
{
int length=strlen(code);
int total = 0;
int*weights=new int[length-1];
for(int i=0;i {
weights[i]=length-i;
}
for(i=0;i {
total+=((code[i]-'0')*weights[i]);
}
switch(total%11)
{
case 0:
if(code[length-1]=='0')

return true;

break;

case 1:
if(toupper(code[length-1])=='X')
{
return true;
}
break;
default:
int check = 11-(total%11);
if(code[length-1]==(check+48))
{
return true;
}
}
delete[]weights;
return false;
} //End of Check Digit Function


And here is the second one to read and output the binary file


#include <iostream.h>
#include <string.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

struct issue{
char recordi[2];
char customeri[6];
char part[7];
char issamt[5];

}issu;


struct despatch{
char recordd[2];
char customerd[6];

}despat;

struct customer{
char recordc[2];
char customerc[6];
char name[21];
char address[61];
char balance[10];
char credit[8];
}custom;


int main(int argc, char* argv[])
{

struct issue *issue1 = new struct issue;
struct despatch *despat1 = new struct despatch;
struct customer *custom1 = new struct customer;

// open the binary file
ifstream input_file("c:\\Bin.bin", ios::in | ios::binary);

input_file.seekg(0L,ios::end);
int num = input_file.tellg()/sizeof(struct issue);
input_file.seekg(0L,ios::beg);
input_file.seekg(0L,ios::end);
int numa = input_file.tellg()/sizeof(struct customer);
input_file.seekg(0L,ios::beg);
input_file.seekg(0L,ios::end);
int numb = input_file.tellg()/sizeof(struct despatch);
input_file.seekg(0L,ios::beg);


while (input_file.peek() != EOF)
{
switch (input_file.peek())
{
case 'I':
case 'R':
input_file.seekg(sizeof(struct issue), ios::cur);
num++;
break;
case 'C':
input_file.seekg(sizeof(struct customer), ios::cur);
numa++;
break;
case 'D':
input_file.seekg(sizeof(struct despatch), ios::cur);
numb++;
break;
}
}
input_file.seekg(0, ios::beg);

// read in the records
for(int i=0; i {
switch (input_file.peek())
{
case 'I':
case 'R':
input_file.read((char*)&issue1[i], sizeof(struct issue));
break;

case 'C':
input_file.read((char*)&custom1[i], sizeof(struct customer));
break;

case 'D':
input_file.read((char*)&despat1[i], sizeof(struct despatch));
break;

}
}


input_file.close();
for(i=0;i {
switch (issue1[i].recordi[0])
{
case 'I':
case 'R':
cout << issue1[i].recordi << endl
<< issue1[i].customeri << endl
<< issue1[i].part << endl
<< issue1[i].issamt << endl << endl;
break;
}

switch(despat1[i].recordd[0])
{
case 'D':
cout << despat1[i].recordd << endl
<< despat1[i].customerd << endl << endl;
break;
}
switch(custom1[i].recordc[0])
{
case 'C':
cout << custom1[i].recordc << endl
<< custom1[i].customerc << endl
<< custom1[i].name << endl
<< custom1[i].address << endl
<< custom1[i].balance << endl
<< custom1[i].credit << endl << endl;
break;
}


}

delete [] issue1;
delete [] despat1;
delete [] custom1;

return 0;



input_file.close();

}

inkedmn
04-27-2003, 12:07 AM
um...

that's aLOT of code :)

try tossing some [code] tags around that stuff, make it a bit easier to read...

[edit]

oh, and what are those #include statements supposed to be including?