GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Feb-2008, 08:49
chagalam2001 chagalam2001 is offline
New Member
 
Join Date: Jun 2007
Posts: 20
chagalam2001 is an unknown quantity at this point

Not reading the contents of the text file


I am having the following problem with the code here. Somehow its not reading the contents of the text file I provide from the cmd line. It just seems to be stuck when executing....

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlcli1.h>
#include <sqlcli.h>
#include <sqlcodes.h>
#include <sqlenv.h>
#include "utilcli.h" /* Header file for CLI sample code */
 
FILE *fp, *fpo ;
//      FILE *fp;
char *buf[40];
 
void print_err( 
    SQLHENV henv, 
    SQLHDBC hdbc, 
    SQLHSTMT hstmt );  
 
int DbBasicConnect(SQLHANDLE, char *, char *, char *, char *);
int  cleanup(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt );
char fname[40]; 
 SQLHANDLE henv;
 SQLHSTMT hstmt = SQL_NULL_HSTMT;
 
int main(int argc, char *argv[])
{
  SQLRETURN cliRC = SQL_SUCCESS;
  int rc = 0;
// 2608  SQLHANDLE henv; /* environment handle */
  SQLHANDLE hdbc; /* connection handle */
// 2608        SQLHSTMT hstmt = SQL_NULL_HSTMT;
  char dbAlias[SQL_MAX_DSN_LENGTH + 1];
  char user[MAX_UID_LENGTH + 1];
  char pswd[MAX_PWD_LENGTH + 1];
  char fname;
  /* check the command line arguments */
  rc = CmdLineArgsCheck1(argc, argv, dbAlias, user, pswd, argv[4]);
    if (rc != 0)
      {
          return rc;
      }
//  printf("\nTHIS SAMPLE SHOWS ");
//  printf("HOW TO CONNECT TO AND DISCONNECT FROM A DATABASE.\n");
 
  /* allocate an environment handle */
  cliRC = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  if (cliRC != SQL_SUCCESS)
  {
    printf("\n--ERROR while allocating the environment handle.\n");
    printf("  cliRC = %d\n", cliRC);
    printf("  line  = %d\n", __LINE__);
    printf("  file  = %s\n", __FILE__);
    return 1;
  }
 
  /* set attribute to enable application to run as ODBC 3.0 application */
  cliRC = SQLSetEnvAttr(henv,
                     SQL_ATTR_ODBC_VERSION,
                     (void *)SQL_OV_ODBC3,
                     0);
  ENV_HANDLE_CHECK(henv, cliRC);
 
  /* connect to a database with SQLConnect() */
  /* this is the basic connection */
        rc = DbBasicConnect(henv, dbAlias, user, pswd, argv[4] );
  if (rc != 0)
  {
    return rc;
  }
      /* free the environment handle */
      cliRC = SQLFreeHandle(SQL_HANDLE_ENV, henv);
      return 0;
            } /* main */
 
/* connect to a database with a basic connection using SQLConnect() */
 
        int DbBasicConnect(SQLHANDLE henv,
                   char db1Alias[],
                   char user[],
                   char pswd[], char *fname)
                {
  SQLRETURN cliRC = SQL_SUCCESS;
 
  int rc = 0;
  SQLHANDLE hdbc; /* connection handle */
  SQLINTEGER length1; /*for bindcol */
  int i=0; 
  /* allocate a database connection handle */
  cliRC = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  ENV_HANDLE_CHECK(henv, cliRC);
  SQLHANDLE hstmt;
 
//  printf("\n  Connecting to the database %s ...\n", db1Alias);
  /* connect to the database */
/*  cliRC = SQLConnect(hdbc,
                     (SQLCHAR *)db1Alias,
                     SQL_NTS,
                     (SQLCHAR *)user,
                     SQL_NTS,
                     (SQLCHAR *)pswd,
                     SQL_NTS);
  DBC_HANDLE_CHECK(hdbc, cliRC);
  printf("  Connected to the database %s.\n", db1Alias);
*/
     /*If file doesn't exist or filetype isn't allowed exit and*/
     /*error message & return (1) control to the OS*/
 if ((fp=fopen(fname, "rt"))==NULL)
 {
     printf("Error, can not open iFile %s",fname );
     exit(0);
 }
    printf ("Input File: %s\n" ,fname);
 SQLCHAR *stmt = (SQLCHAR *) "SELECT employeename where SNO in (?) ";
       printf("    Source \n");
         printf("    -------- ----\n");
 
  char str[28];/* read in the records */ 
  char docid[28];/* docid for bindcol */ 
  char outputfile[36]; /* output docid records */
  SQLCHAR *bstr;
  strcpy(outputfile,"");
  strcpy (outputfile,fname);
  strcat (outputfile,".docids");
  printf("Output File: %s\n",outputfile);
 
 if ((fpo=fopen(outputfile, "w"))==NULL)
 {
     printf("Error, can not open oFile %s\n",outputfile );
     exit(0);
 }
 
 bstr = (SQLCHAR *) malloc((sizeof(char))*28);
 
/*ALLOCAT ENV() */
  rc = SQLAllocEnv( &henv );
  if( rc != SQL_SUCCESS )
  {
      goto exit;
  }
  rc = SQLAllocConnect( henv, &hdbc );
  if( rc != SQL_SUCCESS )
  {
     goto exit;
  }
 
/*  rc = SQLConnect(
            hdbc,
            DSN,
            SQL_NTS,
            user,
            SQL_NTS,
            pswd,
            SQL_NTS );
 
  if( rc != SQL_SUCCESS )
  {
      printf("connection failed\n");
      goto exit;
  }
*/
  printf("\n  Connecting to the database %s ...\n", db1Alias);
 
  /* connect to the database */
  rc = SQLConnect(hdbc,
                     (SQLCHAR *)db1Alias,
                     SQL_NTS,
                     (SQLCHAR *)user,
                     SQL_NTS,
                     (SQLCHAR *)pswd,
                     SQL_NTS);
// 2508  DBC_HANDLE_CHECK(hdbc, cliRC);
        DBC_HANDLE_CHECK(hdbc, rc);
  printf("  Connected to the database %s.\n", db1Alias);
 
  /* else printf("connection ok\n");*/
 
/* Allocate the statement */
  rc=SQLAllocStmt(hdbc, &hstmt);
  if(rc!=SQL_SUCCESS)
  {
     printf("error in allocstmthandle\n");
     goto exit;
  }
  else printf("Handle alloc ok\n"); 
 
  /* set CURSOR_TYPE has to be done before the prepare! */
  rc=SQLSetStmtAttr(hstmt,
  SQL_ATTR_CURSOR_TYPE,
  (SQLPOINTER) SQL_CURSOR_STATIC,
  0);
  if(rc!=0)
     printf("error in sql_attr_cursor_type setting\n");
 
/*PREPARE*/
  rc=SQLPrepare(hstmt, (SQLCHAR*)stmt, SQL_NTS);
  if(rc!=SQL_SUCCESS)
  {
     printf("Prepare ERROR \n");
     goto exit;
  }
  /* else printf("Prepare OK\n"); */
 
while (fgets(str,27,fp)!=NULL)
{
  /* printf("STRLEN=%d\n",strlen(str)); */
  if (strlen(str)>1)
  {  
     strcpy ((SQLPOINTER)bstr, "");
     strcpy ((SQLPOINTER)bstr, str);
   /*  printf("bstr=str=%s\n",bstr); */
 
/* bind the value for doc_ctrl_id */
  rc=SQLBindParameter( hstmt,
                       1,
                       SQL_PARAM_INPUT, SQL_C_CHAR , SQL_CHAR,
                       27, 0,
                       (SQLPOINTER)bstr, 27, NULL);
  if(rc!=SQL_SUCCESS)
  {
     printf("BindParam ERROR \n");
     goto exit;
  }
 
   /* Execute()*/
  rc = SQLExecute( hstmt );
  if( rc != SQL_SUCCESS )
  {
       printf("Execute ERROR \n");
//       print_err( henv, hdbc, hstmt );
       goto exit; 
  }
  /* else printf("Execute OK\n");*/
 
   rc = SQLBindCol( 
        hstmt, 
        1, 
        SQL_C_CHAR, 
        &docid, 
        sizeof( docid ), 
        &length1 ); 
   if( rc != SQL_SUCCESS ) 
   { 
       printf("BindCol failed\n");
       goto exit; 
   } 
   /* else printf ("bindCol ok\n"); */
   
 
   /* rc = SQLFetch( hstmt ); 
   if( rc == SQL_ERROR ) 
   { 
       printf("Fetch failed\n");
       goto exit; 
   } 
   else 
   { 
       printf("docid %s\n", docid );
   }*/
 
   while (SQLFetch(hstmt)== SQL_SUCCESS )
   {
        printf("docid %s\n", docid );
       fputs(docid,fpo);
       fputs("\n",fpo);
   }
 
 
 rc=SQLCloseCursor(hstmt); 
   if(rc != SQL_SUCCESS)
     printf ("sqlclose failed\n");
 
  if (i++%1000==0)
  {
      printf ("%d\n",i);
  }
  free(bstr); 
 }/* if(strlen(str)>1) */
}/* while (fgets....) */
 
rc=cleanup(henv, hdbc, hstmt);
printf("Cleanup rc=%d\n", rc);
return 0;
 
exit:
/*    if( rc == SQL_SUCCESS_WITH_INFO || rc == SQL_ERROR )
    {
        print_err( henv, hdbc, hstmt );
    }
*/
    return( (int)rc );
 
   }
 
 
 
 int cleanup(SQLHENV henv, SQLHDBC hdbc, SQLHSTMT hstmt ) 
{ 
/* 
 *  Close cursor and free bound columns. 
 */ 
   int rc=0; 
     
   /*LARGE_INTEGER st2, et2, pf2=LiInit; 
 
   QueryPerformanceCounter (&st2);*/ 
 
    rc = SQLFreeStmt( hstmt, SQL_CLOSE ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
 
    rc = SQLFreeStmt( hstmt, SQL_UNBIND ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
 
    rc = SQLTransact( henv, hdbc, SQL_COMMIT ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
 
/* 
 *  Disconnect and free up CLI resources. 
 */ 
 
    rc = SQLFreeStmt( hstmt, SQL_DROP ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
    hstmt = SQL_NULL_HSTMT; 
 
    rc = SQLDisconnect( hdbc ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
  
    rc = SQLFreeConnect( hdbc ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
 
    hdbc = SQL_NULL_HDBC; 
 
    rc = SQLFreeEnv( henv ); 
    if( rc != SQL_SUCCESS ) 
    { 
        goto exit; 
    } 
 
/* 
 *  Print out error if any and exit program. 
 */ 
 
exit: 
 
    if( rc == SQL_SUCCESS_WITH_INFO || rc == SQL_ERROR ) 
    { 
        printf("print_err Execute ERROR \n");   
//        print_err( henv, hdbc, hstmt ); 
    } 
    return( (int)rc ); 
  }
Last edited by admin : 06-Feb-2008 at 17:35. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with running time ktg C Programming Language 10 26-Oct-2007 07:27
apache 2.2.1 doesn't support nested query ctrohaya Apache Web Server Forum 0 15-Nov-2006 19:46
Net-ok.net - Traceroute, Ping, Domain Name Server (DNS) Lookup, WHOIS, and DNS Records Lookup Network Query Tool netoknet Web Hosting Forum 1 19-Jan-2006 01:37
How to know when some program is running. stormlab C++ Forum 2 11-Oct-2005 08:15
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 10:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:40.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.