Solved

How can I loop versus hardcode?

Posted on 2003-11-18
5
463 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:fgs3124
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9772108
What about

for ( long l = 0; l < 36; ++l) {

    fprintf(fout,"%s, \n", UC _bstr_t(RsITEM(spRS,l)));
}

?
0
 

Author Comment

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

Thank you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9772729
You are most welcome :o)
0
 

Expert Comment

by:FatNewbie
ID: 9777150
you can't just make people rewrite code for u.
or write some new stuff
0
 

Author Comment

by:fgs3124
ID: 9779173
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 . . . ?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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