Reading a file

M

muser

The following program reads two lines of a file in my A drive and
reads no more.
Can some sarmartian tell me why that is. Please copy and paste the
program into your own compiler please.

program is this.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>


using namespace std;

struct crecord {
long customercode[5];
char customername[21];
char customeraddress[61];
double customerbalance;
double creditlimit;
};

struct irrecord {
long customercode[5];
long partnum[5];
long issue_rec;
};

struct drecord {
long customercode;
};


////////////////////////////////////////////////////////////
bool IRRecordvalidlength(char* record)
{
int len;

switch(record[0])
{
case'i':
case'I':
case'r':
case'R':

len= strlen(record);
if(len<17)
return false;
break;


}

return true;
};
////////////////////////////////////////////////////////////
bool CRecordvalidlength(char* cstring)
{
int len;

switch(cstring[0])
{
case'c':
case'C':

len = strlen(cstring);
if(len<103)
return false;
break;

}


return true;

}
////////////////////////////////////////////////////////
bool Drecordvalidlength(char* record)
{
int len;

switch(record[0])
{
case'd':
case'D':

len = strlen(record);
if(len<7)
return false;
break;

}



return true;
}
//////////////////////////////////////////////////////////
bool CheckAllNumeric( const char * string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string ) )
return false;

return true;
}

////////////////////////////////////////////////////////////////
bool CheckFloatNumber( const char* string )
{
int Len = strlen( string );

for( int i = 0; i < Len; ++i )
if( !isdigit( string ) &&
string != '.' )
return false;

return true;
}

///////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////

crecord Newcrecord;
bool CheckAddress( char* alpha )
{

int max_alpha = 60;

for( int i = 0; i < max_alpha; ++i )
if( !isalpha( alpha ) && ( alpha != ';;;;' ) && ( alpha !=
' ' ))
if(Newcrecord.customeraddress[61] != (alpha))
return false;

return true;


}
///////////////////////////////////////////////////////////////////////////////
bool CheckAddressN( char* record )
{

char* AN[4];
char* BAN;

strncpy(AN[4], &record[27], 4);
AN[4] = '\0';


for(int i=0; i < 4; i++)

if(AN = " ") // check that last character is a number and the
next character
{
//is a char.
BAN = (AN + 1 ) ;
if(!isalpha(BAN)){}

}
return false;

if(AN = " ") // check that last character is a number and the
next character
{
//is a char.
BAN = (AN - 1);
if(!isdigit(BAN)){};

}
return false;


if(AN = ";")
{

BAN = (AN - 1);

if(!isdigit(BAN)){};
}
return false;

if(AN = ";")
{
BAN = (AN + 1);

if(!isalpha(BAN)){};

}
return false;

if(AN = ";")
{
BAN = (AN + 1);

for(int i=0; i < 17; i++)
if(!isalpha(BAN))
return false;
}

return true;

}







////////////////////////////////////////////////////////////////

bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{
char customercode[6];
char balance[10];
char limit[10];
char Addresscheck[4];

crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[1], 5 );
customercode[5] = '\0';
if( !CheckAllNumeric( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.customercode[5] = atol( customercode );

//
// process the customer name
//
strncpy( Newcrecord.customername, &record[7], 20 );
Newcrecord.customername[20] = '\0';

//
// process the customer address
//
strncpy( Newcrecord.customeraddress, &record[27], 60 );
Newcrecord.customeraddress[60] = '\0';

if(!CheckAddressN(Addresscheck))
return false;

//
// process the customer balance
//
strncpy( balance, &record[87], 9 );
balance[9] = '\0';
if( !CheckFloatNumber( balance ) ) {
prnfile << "Invalid: balance field is not a valid number [position
88 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.customerbalance = atof( balance );

validdata.write((char*) record, Newcrecord.customerbalance );

//
// process the customer limit
//
strncpy( limit, &record[97], 9 );
limit[9] = '\0';
if( !CheckFloatNumber( limit ) ) {
prnfile << "Invalid: limit field is not a valid number [position
98 - 9 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.creditlimit = atof( limit );

validdata.write((char*) record, Newcrecord.creditlimit );

return true;

}

///////////////////////////////////////////////////////

bool processdrecord(ofstream& prnfile, ofstream& validdata, char*
record)
{
drecord NewDrecord;
char customercode[6];

strncpy( customercode, &record[2], 5);
customercode[6] = '\0';
if(!CheckAllNumeric( customercode )){
prnfile<< "Invalid: D record customer code does not contain
numerical format"<<endl;
prnfile<< record <<endl;
return false;
}
NewDrecord.customercode = atol( customercode );

validdata.write((char*) record, NewDrecord.customercode);

return true;
}

/////////////////////////////////////////////////////////////////////////
bool ProcessIRecord( ofstream& prnfile, ofstream& validdata, char*
record )
{


char customer[6];
char issue_rec[5];
char code[4];
char part[6];

long customer_code;
long issuerec;
long partnum[6];



//
// process the customer code
//
strncpy( customer, &record[2], 5 );
customer[6] = '\0';
if( !CheckAllNumeric( customer ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
customer_code = atol( customer );

//
// process the part number
//
strncpy( part, &record[7], 6 );
part[6] = '\0';
if( !CheckAllNumeric( part ) ) {
prnfile << "Invalid: part number needs to be all numeric [position
9 - 6 characters]:\n";
prnfile << record << endl;
return false;
}
partnum[6] = atol( part );

validdata.write((char*) record, partnum[6]);

return true;

//
// process the third number
//
strncpy( issue_rec, &record[13], 4 );
code[4] = '\0';
if( !CheckAllNumeric( issue_rec ) ) {
prnfile << "Invalid: the code needs to be all numeric [position 16
- 4 characters]:\n";
prnfile << record << endl;
return false;
}
issuerec = atol( issue_rec );


return true;
}
///////////////////////////////////////////////////////////////////////////
bool CheckDigit(ofstream& prnfile, char* record)
{

int weightingfactor;
int remainder;
int weightitem1 = 0;
int weightitem2 = 0;
int weightitem3 = 0;
int weightitem4 = 0;
int weightitem5 = 0;
int product;
int Mod11 = 11;
int checkdigit;
char partnum[6];

irrecord Newirrecord;


strncpy(partnum, &record[7], 6);
partnum[5] = '\0';
Newirrecord.partnum[5] = atol( partnum );


weightingfactor = 6;


weightingfactor = (Newirrecord.partnum[1] * weightingfactor);

weightingfactor = 5;

weightitem2 = (Newirrecord.partnum[2] * weightingfactor);

weightingfactor = 4;

weightitem3 = (Newirrecord.partnum[3] * weightingfactor);

weightingfactor = 3;

weightitem4 = (Newirrecord.partnum[4] * weightingfactor);

weightingfactor = 2;

weightitem5 = (Newirrecord.partnum[5] * weightingfactor);


//int weigthinfactor = 6;
//int product = 0;
//for(int i = 1; i <= 5; ++i) {
// product += Newicrecord.partnum * weigthingfactor;
// weigtingfactor--;
//}
//> remainder = (product % Mod11);

//I had to hunt around to find how Mod11 was defined. What's wrong
with
// remainder = product % 11;

product = (weightitem1 + weightitem2 + weightitem3 + weightitem4 +
weightitem5);

remainder = (product % Mod11);

checkdigit = (Mod11 - remainder);
if(! Newirrecord.partnum[5] == checkdigit){
prnfile<< "Invalid part number";
prnfile<< record << endl;
}
return false;


return true;

}


/////////////////////////////////////////////////////////////////////////
bool CheckDigitForCustomerCode(ofstream& prnfile, char* record)
{
int weightfactor;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
int counter4 = 0;
int remainder;
int prod;
int Mod11 = 11;
int check_digit;
char custcode[5];
int cust_code[5] = { 0, 0, 0, 0, 0 };



strncpy(custcode, &record[2], 5);
custcode[5] = '\0';
cust_code[5] = atoi( custcode );

weightfactor = 5;

counter1 = cust_code[1] * weightfactor;

weightfactor = 4;

counter2 = cust_code[2] * weightfactor;

weightfactor = 3;

counter3 = cust_code[3] * weightfactor;

weightfactor = 2;

counter4 = cust_code[4] * weightfactor;

prod = (counter1 + counter2 + counter3 + counter4);

remainder = prod % Mod11;

check_digit = (Mod11 - remainder);

if(!cust_code[5] == check_digit)
return false;


return true;

}


int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("A:\\514650TD.txt", ios::in);
if(!testdata)
{
cout<<"The file does not already exist" << infile << endl;
return EXIT_FAILURE;
}

validdata.open("A:\\514650.DAT", ios::eek:ut | ios::binary);
if(!validdata.is_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("A:\\514650.TXT", ios::eek:ut);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};


prnfile << "C_RECORD, I-R RECORD, D_RECORD ERRORS" << endl;

while( testdata.getline( temp1, sizeof(temp1)))
{
if(!CRecordvalidlength( temp1 )){
prnfile<<"Invalid: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
};
if(!IRRecordvalidlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};
if(!Drecordvalidlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};

switch( temp1[0] )
{
case 'c':
case 'C':
Processcrecord( prnfile, validdata, temp1 );
CheckDigitForCustomerCode( prnfile, temp1 );
break;

case 'i':
case 'I':
case 'r':
case 'R':
ProcessIRecord( prnfile, validdata, temp1 );
break;

case 'd':
case 'D':
processdrecord( prnfile, validdata, temp1 );
break;
default: prnfile<< "Unknown record";
prnfile<< temp1 << endl;
};


testdata.close();
validdata.close();
prnfile.close();

};
return EXIT_SUCCESS;

};

The file it is reading contains the following.


c23454stevelaw 12sceptrerd;Ilford;Essex;IG39By;
0000.25001600
D23456 etryteryyeyte
ir345691234551244
C234551212refckfklioiUIFJNGFJKGGJKFGJ
 
R

Ron Natalie

muser said:
testdata.close();
validdata.close();
prnfile.close();
These closes seem to be inside the while( testdata.getline( temp1, sizeof(temp1)))
loop. I think the reason why you get through the first line unscathed is that it hits
the continue (which continues the loop without executing these lines). The second
time through you close the files.

If you'd have indented things consistently you may have caught this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Reading a file. 3
function error 6
The program/ header file contents 2
Access violation error 10
passing a parameter 2
inialisation problem. 2
Logical error/ whole program contained within. 4
function code 2

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top