?
Solved

How can I loop versus hardcode?

Posted on 2003-11-18
5
Medium Priority
?
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

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