I am working on a small program C++. I have information obtained in one function that will be used by many other functions. I am considering using a structure to store the information but I don't know how to share it between functions.
here is an example: first the header file
// public_dial.h
#define MAXINPUT 128 // Maximum string length everywhere
#define RESPLEN 2000 // Maximum response length
int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags);
typedef struct //_tagCCommand
{
public:
char m_szhost_name[MAXINPUT];
char m_szuser_name[MAXINPUT];
char m_zpassword[MAXINPUT];
char m_zdb_name[MAXINPUT];
unsigned int m_nport_num;
char m_zsocket_name[MAXINPUT];
unsigned int flags;
} CCommand;
here is the .cpp
//-----------------------------------------------------------------------//
// P_Dial_port_test.cpp //
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <mysql.h>
#include "mysql_common.h"
#include "public_dial.h"
#include "getopt.h"
using namespace std;
#define def_host_name NULL // host to connect to (default = localhost)
#define def_user_name NULL // User name (default = your login name)
#define def_password NULL // password (default = none)
#define def_db_name NULL // database to use (default = none)
#define def_port_num 0 // use default port
#define def_socket_name NULL // use default socket
#define def_loc NULL // use default city
#define def_speed NULL // use default speed
#define def_tel_num NULL // use default tel number
#define def_ckt_num NULL // use default ckt number
char *groups[] = {"client", NULL};
struct option long_options[] =
{
{"host", required_argument, NULL, 'h'},
{"user", required_argument, NULL, 'u'},
{"password", required_argument, NULL, 'p'},
{"port", required_argument, NULL, 'P'},
{"socket", required_argument, NULL, 'S'},
{"location", required_argument, NULL, 'l'},
{"speed", required_argument, NULL, 's'},
{"tel", required_argument, NULL, 't'},
{"ckt", required_argument, NULL, 'c'},
{0,0,0,0}
};
MYSQL *conn; // pointer to connection handler
int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags)
{
cout << "at Start" << endl;
if(host_name != NULL) cout << host_name << endl;
if(user_name != NULL) cout << user_name << endl;
if(password != NULL) cout << password << endl;
if(db_name != NULL) cout << db_name << endl;
if(port_num != NULL) cout << port_num << endl;
if(socket_name != NULL) cout << socket_name << endl;
if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;
cout << "end start" << endl;
conn = do_connect(host_name, user_name, password, db_name, port_num, socket_name, 0);
if(conn == NULL)
return 1;
//do real work here
do_disconnect(conn);
return 0;
// return 0;
}
int main (int argc, char *argv[])
{
char *host_name = def_host_name;
char *user_name = def_user_name;
char *password = def_password;
unsigned int port_num = def_port_num;
char *socket_name = def_socket_name;
char *db_name = def_db_name;
char passbuf[100];
int ask_password = 0;
int i;
int c, option_index;
my_init();
CCommand *pCmd;
// load_defaults("my", groups, &argc, &argv); //removed because will not be using conf file
while((c =getopt_long (argc, argv, "h:u:S:", long_options, &option_index)) != EOF)
{
switch (c)
{
case 'h':
host_name = optarg;
cout << "host_name_ =" << host_name << endl;
strcpy(pCmd->m_szhost_name,host_name);
cout << "pCmd->m_szhost_name =" << pCmd->m_szhost_name << endl;
break;
case 'u':
user_name = optarg;
break;
case 'p':
if(!optarg) //no value given
ask_password = 1;
else
{
(void) strncpy (passbuf, optarg, sizeof(passbuf) -1);
passbuf[sizeof(passbuf) -1] = '\0';
password = passbuf;
while (*optarg)
*optarg ++ = ' ';
}
break;
case 'P':
port_num = atoi (optarg);
break;
case 'S':
socket_name = optarg;
break;
}
}
argc -= optind; //advance past the arguments that were processed
argv += optind; //by getopt_long
if(argc > 0)
{
db_name = argv[0];
--argc; ++argv;
}
if(ask_password)
password = get_tty_password(NULL);
start(host_name, user_name, password, db_name, port_num, socket_name, 0);
return 0;
}
the code does not compile because of the print statement "if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;
cout << "end start" << endl;"
in function int start(
How would I get at the structure data entered in the main function using the start function.
Thanks in advance
here is an example: first the header file
// public_dial.h
#define MAXINPUT 128 // Maximum string length everywhere
#define RESPLEN 2000 // Maximum response length
int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags);
typedef struct //_tagCCommand
{
public:
char m_szhost_name[MAXINPUT];
char m_szuser_name[MAXINPUT];
char m_zpassword[MAXINPUT];
char m_zdb_name[MAXINPUT];
unsigned int m_nport_num;
char m_zsocket_name[MAXINPUT];
unsigned int flags;
} CCommand;
here is the .cpp
//-----------------------------------------------------------------------//
// P_Dial_port_test.cpp //
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <mysql.h>
#include "mysql_common.h"
#include "public_dial.h"
#include "getopt.h"
using namespace std;
#define def_host_name NULL // host to connect to (default = localhost)
#define def_user_name NULL // User name (default = your login name)
#define def_password NULL // password (default = none)
#define def_db_name NULL // database to use (default = none)
#define def_port_num 0 // use default port
#define def_socket_name NULL // use default socket
#define def_loc NULL // use default city
#define def_speed NULL // use default speed
#define def_tel_num NULL // use default tel number
#define def_ckt_num NULL // use default ckt number
char *groups[] = {"client", NULL};
struct option long_options[] =
{
{"host", required_argument, NULL, 'h'},
{"user", required_argument, NULL, 'u'},
{"password", required_argument, NULL, 'p'},
{"port", required_argument, NULL, 'P'},
{"socket", required_argument, NULL, 'S'},
{"location", required_argument, NULL, 'l'},
{"speed", required_argument, NULL, 's'},
{"tel", required_argument, NULL, 't'},
{"ckt", required_argument, NULL, 'c'},
{0,0,0,0}
};
MYSQL *conn; // pointer to connection handler
int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags)
{
cout << "at Start" << endl;
if(host_name != NULL) cout << host_name << endl;
if(user_name != NULL) cout << user_name << endl;
if(password != NULL) cout << password << endl;
if(db_name != NULL) cout << db_name << endl;
if(port_num != NULL) cout << port_num << endl;
if(socket_name != NULL) cout << socket_name << endl;
if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;
cout << "end start" << endl;
conn = do_connect(host_name, user_name, password, db_name, port_num, socket_name, 0);
if(conn == NULL)
return 1;
//do real work here
do_disconnect(conn);
return 0;
// return 0;
}
int main (int argc, char *argv[])
{
char *host_name = def_host_name;
char *user_name = def_user_name;
char *password = def_password;
unsigned int port_num = def_port_num;
char *socket_name = def_socket_name;
char *db_name = def_db_name;
char passbuf[100];
int ask_password = 0;
int i;
int c, option_index;
my_init();
CCommand *pCmd;
// load_defaults("my", groups, &argc, &argv); //removed because will not be using conf file
while((c =getopt_long (argc, argv, "h:u:S:", long_options, &option_index)) != EOF)
{
switch (c)
{
case 'h':
host_name = optarg;
cout << "host_name_ =" << host_name << endl;
strcpy(pCmd->m_szhost_name,host_name);
cout << "pCmd->m_szhost_name =" << pCmd->m_szhost_name << endl;
break;
case 'u':
user_name = optarg;
break;
case 'p':
if(!optarg) //no value given
ask_password = 1;
else
{
(void) strncpy (passbuf, optarg, sizeof(passbuf) -1);
passbuf[sizeof(passbuf) -1] = '\0';
password = passbuf;
while (*optarg)
*optarg ++ = ' ';
}
break;
case 'P':
port_num = atoi (optarg);
break;
case 'S':
socket_name = optarg;
break;
}
}
argc -= optind; //advance past the arguments that were processed
argv += optind; //by getopt_long
if(argc > 0)
{
db_name = argv[0];
--argc; ++argv;
}
if(ask_password)
password = get_tty_password(NULL);
start(host_name, user_name, password, db_name, port_num, socket_name, 0);
return 0;
}
the code does not compile because of the print statement "if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;
cout << "end start" << endl;"
in function int start(
How would I get at the structure data entered in the main function using the start function.
Thanks in advance