Solved

Experts-please review my function and see if it is "good" enough..!

Posted on 2000-03-27
3
227 Views
Last Modified: 2013-11-20
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_NOFIELD1, szBuffer1); /* for H */
iErr = DoGetBuffer(szFile,SZ_NOFIELD2, szBuffer2); /* for D */
iErr = DoGetBuffer(szFile,SZ_NOFIELD3, 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(CFile * 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;

}
----------------------------------
0
Comment
Question by:Haho
3 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2663699
you may want to add some exception handling as CFile::Read(...) can throw a CFileException .

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)
0
 

Accepted Solution

by:
wasan earned 100 total points
ID: 2664956
Hi,

Make sure that you have only one return
statement in your function.

You can make the following change to do it.

int iRc = -1;


    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';
iRc = 0;  // changed by wasan
}
//follwing two lines added by wasan
if ( iRc == 0 )
break;
// 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 iRc; //changed by wasan

bye,
wasan.

   
0
 
LVL 2

Expert Comment

by:DKostov
ID: 2665520
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 106
iSeries FTP Exit Program 8 124
viewing source code from eclipse 13 88
countEvens challenge 2 76
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now