|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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?
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#3
|
||||
|
||||
|
Quote:
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. |
|
#4
|
|||
|
|||
|
thnx, the + was what i was looking for
![]() |
|
#5
|
|||
|
|||
|
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*) |
|
#6
|
||||
|
||||
|
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 [/edit] |
|
#7
|
|||
|
|||
|
Quote:
Damn that was fast, not even one second after i posted ![]() |
|
#8
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > String Constants |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|