C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2003, 05:43 PM
ZeRO ZeRO is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 64 ZeRO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old June 9th, 2003, 06:56 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
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?

Reply With Quote
  #3  
Old June 9th, 2003, 07:06 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,864 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 23 h 39 m 49 sec
Reputation Power: 462
Quote:
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.

Reply With Quote
  #4  
Old June 9th, 2003, 07:26 PM
ZeRO ZeRO is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 64 ZeRO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
thnx, the + was what i was looking for

Reply With Quote
  #5  
Old June 9th, 2003, 07:30 PM
ZeRO ZeRO is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 64 ZeRO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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*)

Reply With Quote
  #6  
Old June 9th, 2003, 07:30 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,588 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 32 m 4 sec
Reputation Power: 1001
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]

Reply With Quote
  #7  
Old June 9th, 2003, 07:31 PM
ZeRO ZeRO is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 64 ZeRO User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Quote:
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

Reply With Quote
  #8  
Old June 10th, 2003, 12:13 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,864 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 23 h 39 m 49 sec
Reputation Power: 462
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > String Constants


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT