#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12

    String Constants


    Im trying to write a program to backup databases, but I just can't get this darn string constants thing down.

    Code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "backup.h"
    
    int main() {
      system("php -q /usr/local/bin/list.php > /etc/list");
        std::string db;
        std::ifstream ifs("/etc/list");
        while (!ifs.eof()) {
            std::getline(ifs, db);
            do_backup(db);
        }
        ifs.close();
        return 0;
    }
    
    int do_backup(const std::string& db){
    system("mysqldump -u root -pxxxx " db " > " db ".sql1");
    return 0;
      }
    Error
    Code:
    [zero@demon dbbackup]$ g++ main.cpp
    main.cpp: In function `int do_backup(const std::string&)':
    main.cpp:35: parse error before string constant
    [zero@demon dbbackup]$
    Please help me :)
  2. #2
  3. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    I am not sure what line 35 is in your program, but from my program:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    //#include "backup.h"
    
    int main()
    {
    	system("php -q /usr/local/bin/list.php > /etc/list");
    	std::string db;
    	std::ifstream ifs("/etc/list");
    	while (!ifs.eof())
    	{
    		std::getline(ifs, db);
    //        do_backup(db);
    	}
    	ifs.close();
    	return 0;
    }
    
    int do_backup(const std::string& db)
    {
    	system("mysqldump -u root -pxxxx " db " > " db ".sql1");
    	return 0;
    }
    I get this error message:

    Code:
    c:\windows\desktop\jason\visual c++\devshed\zero\t64709\src\t64709.cpp(22) : error C2146: syntax error : missing ')' before identifier 'db'
    c:\windows\desktop\jason\visual c++\devshed\zero\t64709\src\t64709.cpp(22) : error C2059: syntax error : ')'
    Specifically, it has a problem with this statement:

    Code:
    	system("mysqldump -u root -pxxxx " db " > " db ".sql1");
    I have never used C++ strings, so I have no idea what is legal or not... anyone else?
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by Jason Doucette
    Specifically, it has a problem with this statement:

    Code:
    	system("mysqldump -u root -pxxxx " db " > " db ".sql1");
    I have never used C++ strings, so I have no idea what is legal or not... anyone else?
    Definitely not kosher. I don't know PHP, but I assume that that is the way it has you concatenate. In C++, you need an explicit operator; in the STL's string class, the concatenation operator is '+':

    Code:
    int do_backup(const std::string& db)
    {
        string cmd = "mysqldump -u root -pxxxx " + db + " > " + db + ".sql1";
        system(cmd);
        return 0;
    }
    I'm quite sure that you could have done the entire concatenation inside of the system function call. I just feel more comfortable this way.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    thnx, the + was what i was looking for:D
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    Now im getting...

    main.cpp: In function `int do_backup(const std::string&)':
    main.cpp:37: no matching function for call to `system(std::string&)'
    /usr/include/stdlib.h:696: candidates are: int system(const char*)
    /usr/include/stdlib.h:696: int system(const char*)
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    What dwise1_aol said above, except that the call to system should read:
    system(cmd.c_str());

    That's because system is expecting a const char * argument and cmd is a string class object. The call to the c_str() method returns a const char * representation of the string.

    [edit]Seems that you found the bug on your own, while I was typing my reply :D[/edit]
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    64
    Rep Power
    12
    Originally posted by Scorpions4ever
    What dwise1_aol said above, except that the call to system should read:
    system(cmd.c_str());

    That's because system is expecting a char * argument and cmd is a string class object. The call to the c_str() method returns a char * representation of the string.
    Damn that was fast, not even one second after i posted :p :D
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    DOH!

    Sorry about that. I never have used the string type since most of my C++ work predates the STL and most of my work these past 8 years has in C, during with time all my C++ work has been with MFC so I have used their CString class. I am always aware of the need to expose the char array in CString when using a C string function, but it slipped my mind here with string.

    But the point I want to make is to say that the standard C library provides many functions that take a string as a parameter. However, those strings are C strings, which are character arrays and are declared as char* . I wish to remind you that every time you use a string object with one of these functions that you will need to use the c_str() method.

IMN logo majestic logo threadwatch logo seochat tools logo