473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reading a file.

With the following code I'm trying to read a text file (infile) and
output inaccuracies to the error file (printerfile). The text file is
written and stored on disk, while the printerfile has to be created
when the program executes. But the compile keeps reading that it can't
find the text file.
Karl you wrote the original program from which this one is but a poor
copy, can you or anyone else enlighten me as to why yours worked and
mine didn't.
The full program code follows beneath what I consider to be the main
problem.
int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650EO.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("infile", ios::in);
if(!testdata.is_open())
{
cout<<"The file does not already exist" << infile << '\n';
return EXIT_FAILURE;
}

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

prnfile.open("printerfile", ios::out || ios::binary);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};
/////////////////////////////////////////////////////////////////////////////
Full program code.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include<string.h>

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[6];
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* dstring)
{
int len;

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

len = strlen(dstring);
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 CheckFloatNumber( 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.customeraddress[61] != (alpha[i]))
return false;

return true;
}
////////////////////////////////////////////////////////////////

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

crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[2], 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';

//
// 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 );

//
// 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 );

return true;

}

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

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

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

return true;
}
bool ProcessIRecord( ofstream& prnfile, 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 );

//
// 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(long part_num[6], char* record)
{

int weightingfactor;
int remainder;
int weightitem1, weightitem2, weightitem3, weightitem4, weightitem5;
int product;
int Mod11 = 11;
int checkdigit;
char partnum[6];
strncpy(partnum, &record[7], 6);
partnum[6] = '\0';
part_num[6] = atol( partnum );

weightingfactor = 6;

weightitem1 = (part_num[1] * weightingfactor);

weightingfactor = 5;

weightitem2 = (part_num[2] * weightingfactor);

weightingfactor = 4;

weightitem3 = (part_num[3] * weightingfactor);

weightingfactor = 3;

weightitem4 = (part_num[4] * weightingfactor);

weightingfactor = 2;

weightitem5 = (part_num[5] * weightingfactor);


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

remainder = (product % Mod11);

checkdigit = (Mod11 - remainder);
if(!part_num[6] == checkdigit)
return false;
return true;

}

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

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

counter1 = (custcode[1] * weightfactor);

weightfactor = 4;

counter2 = (custcode[2] * weightfactor);

weightfactor = 3;

counter3 = (custcode[3] * weightfactor);

weightfactor = 2;

counter4 = (custcode[4] * weightfactor);

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

remainder = (prod % Mod11);

check_digit = (Mod11 - remainder);

if(!custcode[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:\\514650EO.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("infile", ios::in);
if(!testdata.is_open())
{
cout<<"The file does not already exist" << infile << '\n';
return EXIT_FAILURE;
}

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

prnfile.open("printerfile", ios::out || ios::binary);
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;
};

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

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

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

};
return EXIT_SUCCESS;

};
Jul 19 '05 #1
3 3360

muser <ch**********@hotmail.com> wrote in message
news:f9**************************@posting.google.c om...
With the following code I'm trying to read a text file (infile) and
output inaccuracies to the error file (printerfile). The text file is
written and stored on disk, while the printerfile has to be created
when the program executes. But the compile keeps reading that it can't
find the text file.
Karl you wrote the original program from which this one is but a poor
copy, can you or anyone else enlighten me as to why yours worked and
mine didn't.
The full program code follows beneath what I consider to be the main
problem. int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650EO.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("infile", ios::in);
You're trying to open a file named "infile", not
named as the string contained in your array with
an identifier of 'infile'.
testdata.open(infile); /* Note: ios::in is default of 'ifstream' */
if(!testdata.is_open())
{
cout<<"The file does not already exist" << infile << '\n';
return EXIT_FAILURE;
}

validdata.open("outfile", ios::out || ios::binary);
validdata.open(outfile, ios::out || ios::binary);

Note: ios::out is default of 'ofstream'.

Question: Why binary?

if(!validdata.is_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("printerfile", ios::out || ios::binary);
Same issues here.
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};


I didn't look at anything else, this is what jumped out at me.

HTH,
-Mike

Jul 19 '05 #2
muser wrote:
With the following code I'm trying to read a text file (infile) and
output inaccuracies to the error file (printerfile). The text file is
written and stored on disk, while the printerfile has to be created
when the program executes. But the compile keeps reading that it can't
find the text file.
Karl you wrote the original program from which this one is but a poor
copy, can you or anyone else enlighten me as to why yours worked and
mine didn't.
The full program code follows beneath what I consider to be the main
problem.
Sorry if I'm not Karl, but this is a public forum.
int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650EO.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("infile", ios::in);
if(!testdata.is_open())
if (!testdata) {
cout<<"The file does not already exist" << infile << '\n';
return EXIT_FAILURE;
}

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

prnfile.open("printerfile", ios::out || ios::binary);
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};
/////////////////////////////////////////////////////////////////////////////
Full program code.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include<string.h>

using namespace std;

struct crecord {
long customercode[5];
Is this 5 long integers or 5 characters?
char customername[21];
char customeraddress[61];
double customerbalance;
double creditlimit;
};
Don't use "magic numbers". Prefer named constants:
const unsigned int MAX_CUSTOMER_CODES = 5;
const unsigned int MAX_CUSTOMER_NAME_LENGTH = 21;
const unsigned int MAX_CUSTOMER_ADDR_LENGTH = 61;

struct crecord
{
long customercode[MAX_CUSTOMER_CODES];
char customername[MAX_CUSTOMER_NAME_LENGTH];
char customeraddress[MAX_CUSTOMER_ADDR_LENGTH];
// ...
};


struct irrecord {
long customercode[5];
long partnum[6];
long issue_rec;
};
See above comment about magic numbers.


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;
};
Perhaps this could be simplified:
if ( (std::toupper(record[0]) == 'I')
|| (std::toupper(record[0]) == 'R'))
{
if (strlen(record) < MAGIC_NUMBER_17)
return false;
}
else
return true;

////////////////////////////////////////////////////////////
bool CRecordvalidlength(char* cstring) [snip]

//////////////////////////////////////////////////////// bool Drecordvalidlength(char* dstring) [snip]

In the above functions:
1. Use std::toupper() or std::tolower() to convert the case
of a character before comparison. This reduces the number
of comparisons and makes the code easier to read.
2. Don't use a switch statement for only one case. The
switch statement in the above functions will reduce to
one case when using either character case conversion
function.
3. You may want to simplify the contents into a single return
statement that uses boolean logic.
4. Magic numbers again.

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

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

return true;
}
Look at the library function "strtol()".

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

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

return true;
}
The above function is not correct.
Use this test string "1..64"
Look at the library function "strtod()".


/////////////////////////////////////////////////////////////////
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.customeraddress[61] != (alpha[i]))
return false;

return true;
} I don't think this is a valid function either.
Try this test case "; ; ; ; ;aa"
Is this a valid address: "630 Mango Lane"?

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

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

crecord Newcrecord;

//
// process the customer code
//
strncpy( customercode, &record[2], 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 );
The above code could be simplified by using the library
function sscanf() or using stringstream and the extraction
operators.

Magic numbers again.


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

//
// process the customer address
//
strncpy( Newcrecord.customeraddress, &record[27], 60 );
Newcrecord.customeraddress[60] = '\0';
Magic Numbers again. What about validating the customer address?

//
// 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 );
A better method is to use sscanf or strtod.


//
// 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 );
A better method is to use sscanf or strtod.


return true;

}

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

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

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


return true;
}

[snip]

1. Convert all the magic numbers into named constants.
2. Prefer strtol over atol.
3. The strtol, sscanf and stringstream have error checking.
Use them.
4. Since you are using C++, prefer std::string to char *.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3
muser wrote:
With the following code I'm trying to read a text file (infile) and
output inaccuracies to the error file (printerfile). The text file is
written and stored on disk, while the printerfile has to be created
when the program executes. But the compile keeps reading that it can't
find the text file.
Karl you wrote the original program from which this one is but a poor
copy, can you or anyone else enlighten me as to why yours worked and
mine didn't.
The full program code follows beneath what I consider to be the main
problem.
int main()
{
const char infile[] = "A:\\514650TD.txt";
const char outfile[] = "A:\\514650VD.DAT";
const char printerfile[] = "A:\\514650EO.TXT";

char temp1[256];

ifstream testdata;
ofstream validdata;
ofstream prnfile;

testdata.open("infile", ios::in);
if(!testdata.is_open())
{
cout<<"The file does not already exist" << infile << '\n';
return EXIT_FAILURE;
}

validdata.open("outfile", ios::out || ios::binary);
In addition to what others said, that's supposed to be a bitwise OR
operator: '|' Single bar, not double bar.

ios::out | ios::binary

Of course, ios::out is redundant for an ofstream.
if(!validdata.is_open())
{
cout<<" The file could not be opened " << outfile <<endl;
return EXIT_FAILURE;
}

prnfile.open("printerfile", ios::out || ios::binary);
Same issue.
if(!prnfile)
{
cout<< "File could not be opened" << prnfile << endl;
return EXIT_FAILURE;
};


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

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

Similar topics

4
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
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...
19
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...
4
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 =...
6
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...
1
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:...
7
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...
5
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...
6
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.