Solved

How can I loop versus hardcode?

Posted on 2003-11-18
5
458 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
Comment Utility
What about

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

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

?
0
 

Author Comment

by:fgs3124
Comment Utility
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
Comment Utility
You are most welcome :o)
0
 

Expert Comment

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

Author Comment

by:fgs3124
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

9 Experts available now in Live!

Get 1:1 Help Now