|
From: Chris <to...@de...> - 2004-02-09 11:59:06
|
Hi all,
I'm a newbie at C++ so these are probably simple answers, but I can't
work them out.
My first problem is if I don't pass an argument to the script (and
instead prompt for it), when I print it out I get weird results - which
means I can't access the filename that I type in.
My second problem is I can't work out why
tablename = strstr(line, find_string);
doesn't actually get the tablename.
Everything compiles ok, compiler is
g++ (GCC) 3.3.3 20031229 (prerelease) (Debian)
Thanks for any help offered.
The file I'm trying to parse is a simple mysql dump:
=========
DROP TABLE IF EXISTS table_a;
CREATE TABLE table_a (
a int
);
=========
My code looks like this:
==============================
#include "iostream"
#include "fstream"
#define DEBUG 1
using namespace std;
enum { SUCCESS, FAILURE };
void printDebugMsg(char * msg);
void printDebugMsg(char * msg)
{
// Should we be printing a msg? (is DEBUG on?)
// No? return.
#ifdef DEBUG
cout << "DEBUG MSG: " << msg << endl;
#endif
}
int main(int argc, char **argv)
{
char * dbdump_filename[1024];
if (argc > 1)
{
if (argc >= 2)
{
*dbdump_filename = argv[1];
}
} else {
/*
Passing in the dbdump_filename in as an argument works.
But, this isn't working. Although '*dbdump_filename' seems to hold the
right value,
it actually looks like a pointer (not an memory address though!)
when you try to open the file.
:/
*/
cout << "Enter db dump filename: ";
char db_filename[1024];
cin.getline(db_filename, 1024);
*dbdump_filename = db_filename;
}
char debugMsg[1024];
cout << "Db filename: " << *dbdump_filename << endl;
ifstream fp;
fp.open(*dbdump_filename);
if (!fp.is_open())
{
cout << "Unable to open file " << *dbdump_filename << endl;
return FAILURE;
}
cout << "Opening file " << *dbdump_filename << ".." << endl;
int bufferSize = 10240;
char line[bufferSize];
char * tablename;
char find_string[] = "CREATE TABLE";
while (!fp.eof()) {
fp.getline(line, bufferSize);
// printDebugMsg(line);
tablename = strstr(line, find_string);
cout << "Table Name: " << tablename;
cout << endl;
}
fp.close();
cout << endl;
return SUCCESS;
}
|