|
From: P.D. v. Z. <pd...@xs...> - 2004-02-09 12:55:06
|
#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)
{
strcpy(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: ";
cin.getline(dbdump_filename, 1024);
}
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;
}
P.S.: Didn't check the strstr() logic, never used it, and can't be bothered
to look up its signature at the moment :)
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Chris
Sent: Monday, February 09, 2004 12:53 PM
To: dev...@li...
Subject: [Dev-C++] strstr question
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;
}
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|