Link to home
Start Free TrialLog in
Avatar of fgs3124
fgs3124

asked on

How can I loop versus hardcode?

I found and modified a simple C++ program to connect to and create a recordset from a database.  I then write out each record to a text file.  I use the fprintf command and it works fine but I currently have hard-coded in the fprintf line 36 times (see code below) because I don't know how to use a for loop (or something like it).  It would make the code nicer and more professional (at least that's my impression).  Can anyone help?

current code:

#include <stdio.h>
#include <afxdisp.h>

#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace

#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
                                  if (FAILED(_hr)) _com_issue_error(_hr); }

#define RsITEM(rs,x) rs->Fields->Item[_variant_t(x)]->Value
#define UC (char *)
struct      InitOle {
            InitOle()  { ::CoInitialize(NULL); }
            ~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;       // Global Instance to force load/unload of OLE

void main(){

    _RecordsetPtr   spRS;
    _ConnectionPtr  spCON;
      FILE *fout;
      char csvfile[17] = "pdd_criteria.csv";

      if((fout = fopen(csvfile,"w")) == NULL)
            printf("cannot open out file !\n");
      
    try{
        CREATEiNSTANCE(spCON,Connection);
                  
                  // connect to local hmi computer
                  
                  spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
                                                       L"DBQ=ppg_2000.MDB;DefaultDir=C:\\test;";            
                  
                  // connect to process computer
                  spCON->ConnectionString = L"DSN=tpd;"
                                              L"UID=tpd; PWD=tpdtpd;";
                                
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );

                        
            spRS->Open("select * from products where bf_max_qty > 0 order by 1 desc", vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);
       
        while(spRS->adoEOF == false){

            printf("productcode = %s  proddesc = %s \n", UC _bstr_t(RsITEM(spRS,0L)),
                                                 UC _bstr_t(RsITEM(spRS,"bobbintype")));

                        
                        // write string to out file
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,0L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,1L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,2L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,3L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,4L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,5L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,6L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,7L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,8L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,9L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,10L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,11L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,12L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,13L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,14L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,15L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,16L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,17L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,18L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,19L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,20L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,21L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,22L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,23L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,24L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,25L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,26L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,27L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,28L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,29L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,30L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,31L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,32L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,33L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,34L)));
                        fprintf(fout,"%s, \n", UC _bstr_t(RsITEM(spRS,35L)));

                        

            spRS->MoveNext();
        }
        spRS->Close();
        spCON->Close();
       
    }
    catch( _com_error &e){
        _bstr_t bstrSource(e.Source());
        _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ")
            + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
            + _bstr_t(e.Description());
       
        MessageBox(0,bs,bstrSource, MB_OK);
    }          
}
#undef UC
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fgs3124
fgs3124

ASKER

Sheever-me-timbers . . . it worked!  I tried using the for loop but I must have been doing something wrong.

Thank you.
You are most welcome :o)
you can't just make people rewrite code for u.
or write some new stuff
Avatar of fgs3124

ASKER

What is that suppose to mean?  This is a help board and I needed help.  I wrote most of the code but got hung up on a piece of it.  That's the very reason this website exists!  I don't suppose the 'Fat' in your userid refers to what resides between your ears . . . ?