[C++] probleme avec mes string
Résolu/Fermé
popy1970
Messages postés
3
Date d'inscription
samedi 9 juin 2007
Statut
Membre
Dernière intervention
12 juin 2007
-
9 juin 2007 à 18:26
mamiemando Messages postés 32283 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 17 mars 2023 - 12 juin 2007 à 07:49
mamiemando Messages postés 32283 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 17 mars 2023 - 12 juin 2007 à 07:49
A voir également:
- [C++] probleme avec mes string
- Cannot access offset of type string on string - Forum PHP
- Uninitialized string offset ✓ - Forum PHP
- Include string - Forum C++
- Illegal string offset ✓ - Forum PHP
- Unsupported operand types: string + string ✓ - Forum PHP
6 réponses
FLYPKONE
Messages postés
7
Date d'inscription
mardi 5 juin 2007
Statut
Membre
Dernière intervention
10 juin 2009
9 juin 2007 à 18:31
9 juin 2007 à 18:31
je pensais que tu faisai allusion aux string "caleçon " des filles
mamiemando
Messages postés
32283
Date d'inscription
jeudi 12 mai 2005
Statut
Modérateur
Dernière intervention
17 mars 2023
7 572
10 juin 2007 à 01:18
10 juin 2007 à 01:18
Pourquoi tu n'utilises pas tout simplement des std::string et des find ?
Bonne chance
#include <string>
#include <iostream>
#include <stdexcept>
int main(){
const std::string str("tapir:zfgracv@host");
const unsigned
idx_sep_login_pass = str.find(':'),
idx_sep_pass_host = str.find('@');
if(idx_sep_login_pass == std::string::npos
|| idx_sep_pass_host == std::string::npos){
std::cerr << "invalid string: format login:pass@host" << std::endl;
return 1;
}
const std::string
login = str.substr(0,idx_sep_login_pass),
pass = str.substr(idx_sep_login_pass+1,idx_sep_pass_host-idx_sep_login_pass-1),
host = str.substr(idx_sep_pass_host+1);
std::cout << "login = " << login << std::endl
<< "pass = " << pass << std::endl
<< "host = " << host << std::endl;
if(!login.size()) throw std::runtime_error("empty login");
if(!pass.size()) throw std::runtime_error("empty pass");
if(!host.size()) throw std::runtime_error("empty host");
return 0;
}
Bonne chance
popy1970
Messages postés
3
Date d'inscription
samedi 9 juin 2007
Statut
Membre
Dernière intervention
12 juin 2007
11 juin 2007 à 03:22
11 juin 2007 à 03:22
merci du coup de main, je commence a y arriver, voici ce que j'ai fait, presque toutes mes variable, reste comme il faut, mais ma variable DBName devient "" apres la fonction...
je continuer a chercher, mais disons que ta piste de solution c'est averer tres bonner
merci
int GetDBaddr( const char* address, const char* namedb)
{
const std::string str(address);
const unsigned
idx_sep_login_pass = str.find(':'),
idx_sep_pass_host = str.find('@');
if(idx_sep_login_pass == std::string::npos
|| idx_sep_pass_host == std::string::npos){
//
return 1;
}
const std::string
login = str.substr(0,idx_sep_login_pass),
pass = str.substr(idx_sep_login_pass+1,idx_sep_pass_host-idx_sep_login_pass-1),
host = str.substr(idx_sep_pass_host+1);
if(!login.size()) throw std::runtime_error("empty login");
if(!pass.size()) throw std::runtime_error("empty pass");
if(!host.size()) throw std::runtime_error("empty host");
DBUser = login;
DBPass = pass;
DBHost = host;
DBName =namedb;
return 0;
}
dans mon fonction parsedbaddress (telle que mentionner dans mon 1er message) j'ai ajouter ceci
std::string address(hostinfo);
std::string namedb(dbname);
GetDBaddr(address.c_str(), namedb.c_str());
je continuer a chercher, mais disons que ta piste de solution c'est averer tres bonner
merci
mamiemando
Messages postés
32283
Date d'inscription
jeudi 12 mai 2005
Statut
Modérateur
Dernière intervention
17 mars 2023
7 572
12 juin 2007 à 00:39
12 juin 2007 à 00:39
Passe les en paramètre de la fonction
#include <string>
#include <iostream>
#include <stdexcept>
bool read_login_pass_host(
const std::string & str,
std::string & login,
std::string & pass,
std::string & host
){
const unsigned
idx_sep_login_pass = str.find(':'),
idx_sep_pass_host = str.find('@');
if(idx_sep_login_pass == std::string::npos
|| idx_sep_pass_host == std::string::npos){
return false;
}
login = str.substr(0,idx_sep_login_pass);
pass = str.substr(idx_sep_login_pass+1,idx_sep_pass_host-idx_sep_login_pass-1);
host = str.substr(idx_sep_pass_host+1);
if(!login.size()) return false;
if(!pass.size()) return false;
if(!host.size()) return false;
return true; // tout est ok :)
}
int main(){
const std::string str("tapir:zfgracv@host");
std::string login,pass,host;
if(!read_login_pass_host(str,login,pass,host)){
std::cerr << "chaine invalide: " << str << std::endl;
return 1;
}
std::cout << "login = " << login << std::endl
<< "pass = " << pass << std::endl
<< "host = " << host << std::endl;
return 0;
}
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
popy1970
Messages postés
3
Date d'inscription
samedi 9 juin 2007
Statut
Membre
Dernière intervention
12 juin 2007
12 juin 2007 à 05:16
12 juin 2007 à 05:16
Merci beaucoups, avec ta piste de solution, j'ai reussi a regle mon petit probleme,
mamiemando
Messages postés
32283
Date d'inscription
jeudi 12 mai 2005
Statut
Modérateur
Dernière intervention
17 mars 2023
7 572
12 juin 2007 à 07:49
12 juin 2007 à 07:49
Pas de soucis :)