Haho
asked on
Experts-please review my function and see if it is "good" enough..!
hi guys,
I have code a function that reads lines from a file into a buffer based on this file format:
H,1,2,3,4,5,6,7,8,9,0
D,1,2,3,4,5
T,3
H,1,2,3,4,5,6,7,8,9,0
D,1,2,3,4,5
T,3
...more records...
where it can also be:
H,1,2,3,4,
5,6,7,8,9,0
D,1,2
,3,4,5
T,3
where the number of fields is FIXED..(ie. H has 10 data fields, D has 5, T has 1)
My function reads one "record" into a buffer:
iErr = DoGetBuffer(szFile,SZ_NOFI ELD1, szBuffer1); /* for H */
iErr = DoGetBuffer(szFile,SZ_NOFI ELD2, szBuffer2); /* for D */
iErr = DoGetBuffer(szFile,SZ_NOFI ELD3, szBuffer3); /* for T */
Note that I use the no of commas as an indicator. This function is very important and there cannot be any bugs in it... can u guys please go through it and comment/improve upon it so that it is "perfect" for production use?
Thanzs!
-------------------------- ---------- --
int CMyClass::DoGetBuffer(CFil e * myFile, int NoFields, char *buffer)
{
int iComma;
char szElement;
DWORD dwRead;
int iLine;
// initialize
iLine = 0;
iComma = 0;
szElement = NULL;
do
{
dwRead = myFile->Read(&szElement, 1);
// check if end of record
if ( \
((szElement == '\r')&&(iComma == (NoFields - 1)))|| \
((dwRead <= 0)&&(iComma == (NoFields - 1))) \
)
{ // should check for record_type in the next line
buffer[iLine] = '\0';
return 0;
}
// ignore newline and cr
if ((szElement != '\n')&&(szElement != '\r'))
{
buffer[iLine] = szElement;
if (buffer[iLine] == ',') // one comma add
iComma++;
iLine++;
};
}
while (dwRead > 0);
// dwRead returned <= 0
return -1;
}
-------------------------- --------
I have code a function that reads lines from a file into a buffer based on this file format:
H,1,2,3,4,5,6,7,8,9,0
D,1,2,3,4,5
T,3
H,1,2,3,4,5,6,7,8,9,0
D,1,2,3,4,5
T,3
...more records...
where it can also be:
H,1,2,3,4,
5,6,7,8,9,0
D,1,2
,3,4,5
T,3
where the number of fields is FIXED..(ie. H has 10 data fields, D has 5, T has 1)
My function reads one "record" into a buffer:
iErr = DoGetBuffer(szFile,SZ_NOFI
iErr = DoGetBuffer(szFile,SZ_NOFI
iErr = DoGetBuffer(szFile,SZ_NOFI
Note that I use the no of commas as an indicator. This function is very important and there cannot be any bugs in it... can u guys please go through it and comment/improve upon it so that it is "perfect" for production use?
Thanzs!
--------------------------
int CMyClass::DoGetBuffer(CFil
{
int iComma;
char szElement;
DWORD dwRead;
int iLine;
// initialize
iLine = 0;
iComma = 0;
szElement = NULL;
do
{
dwRead = myFile->Read(&szElement, 1);
// check if end of record
if ( \
((szElement == '\r')&&(iComma == (NoFields - 1)))|| \
((dwRead <= 0)&&(iComma == (NoFields - 1))) \
)
{ // should check for record_type in the next line
buffer[iLine] = '\0';
return 0;
}
// ignore newline and cr
if ((szElement != '\n')&&(szElement != '\r'))
{
buffer[iLine] = szElement;
if (buffer[iLine] == ',') // one comma add
iComma++;
iLine++;
};
}
while (dwRead > 0);
// dwRead returned <= 0
return -1;
}
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why you don't make "char* buffer" input
parameter "std::string &buffer" or with MFC-"CString &buffer"? In this
case you'll never exceed the buffer's memory limits.
parameter "std::string &buffer" or with MFC-"CString &buffer"? In this
case you'll never exceed the buffer's memory limits.
You may also wish to put in some tests on buffer i.e. make sure it is not NULL and that you do not exceed its bounds (you may have to pass in the size of the buffer)