473,770 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading a file

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 IRRecordvalidle ngth(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 CRecordvalidlen gth(char* cstring)
{
int len;

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

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

}
return true;

}
////////////////////////////////////////////////////////
bool Drecordvalidlen gth(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[i] ) )
return false;

return true;
}

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

for( int i = 0; i < Len; ++i )
if( !isdigit( string[i] ) &&
string[i] != '.' )
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[i] ) && ( alpha[i] != ';;;;' ) && ( alpha[i] !=
' ' ))
if(Newcrecord.c ustomeraddress[61] != (alpha[i]))
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[i] = " ") // check that last character is a number and the
next character
{
//is a char.
BAN = (AN[i] + 1 ) ;
if(!isalpha(BAN[i])){}

}
return false;

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

}
return false;
if(AN[i] = ";")
{

BAN = (AN[i] - 1);

if(!isdigit(BAN[i])){};
}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

if(!isalpha(BAN[i])){};

}
return false;

if(AN[i] = ";")
{
BAN = (AN[i] + 1);

for(int i=0; i < 17; i++)
if(!isalpha(BAN[i]))
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( !CheckAllNumeri c( customercode ) ) {
prnfile << "Invalid: customer code needs to be all numeric
[position 3 - 5 characters]:\n";
prnfile << record << endl;
return false;
}
Newcrecord.cust omercode[5] = atol( customercode );

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

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

if(!CheckAddres sN(Addresscheck ))
return false;

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

validdata.write ((char*) record, Newcrecord.cust omerbalance );

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

validdata.write ((char*) record, Newcrecord.cred itlimit );

return true;

}

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

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

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

validdata.write ((char*) record, NewDrecord.cust omercode);

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( !CheckAllNumeri c( 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( !CheckAllNumeri c( 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( !CheckAllNumeri c( 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(ofst ream& 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.par tnum[5] = atol( partnum );
weightingfactor = 6;
weightingfactor = (Newirrecord.pa rtnum[1] * weightingfactor );

weightingfactor = 5;

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

weightingfactor = 4;

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

weightingfactor = 3;

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

weightingfactor = 2;

weightitem5 = (Newirrecord.pa rtnum[5] * weightingfactor );
//int weigthinfactor = 6;
//int product = 0;
//for(int i = 1; i <= 5; ++i) {
// product += Newicrecord.par tnum[i] * 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.par tnum[5] == checkdigit){
prnfile<< "Invalid part number";
prnfile<< record << endl;
}
return false;
return true;

}
/////////////////////////////////////////////////////////////////////////
bool CheckDigitForCu stomerCode(ofst ream& 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(custcod e, &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.t xt";
const char outfile[] = "A:\\514650VD.D AT";
const char printerfile[] = "A:\\514650.TXT ";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

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

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

prnfile.open("A :\\514650.TXT", ios::out);
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.getlin e( temp1, sizeof(temp1)))
{
if(!CRecordvali dlength( temp1 )){
prnfile<<"Inval id: record does not contain enough characters:\n";
prnfile<< temp1 <<endl;
continue;
};
if(!IRRecordval idlength( temp1 )){
prnfile<< "Invalid: record does not contain enough characters:\n";
prnfile<< temp1 << endl;
continue;
};
if(!Drecordvali dlength( 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 );
CheckDigitForCu stomerCode( 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;Ilf ord;Essex;IG39B y;
0000.25001600
D23456 etryteryyeyte
ir3456912345512 44
C234551212refck fklioiUIFJNGFJK GGJKFGJ
Jul 19 '05 #1
1 1930

"muser" <ch**********@h otmail.com> wrote in message news:f9******** *************** ***@posting.goo gle.com...


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

These closes seem to be inside the while( testdata.getlin e( 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.
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3066
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
7056
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
19
10376
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9842
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile = tmpfile(); /* write into tmpFile */ ...
6
6372
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
1
2019
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6063
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
5
14992
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3531
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
2842
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the wireless where I'm working could just be ungodly slow-- which it is.) Is reading a file much more resource/processor intensive than, say, including a .php file? What about the act of creating a simpleXML object? What about the act of checking the...
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.