Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

Sujet : substr sous linux [ Archives / Divers ] (juliusius37)

vendredi 13 février 2004 à 10:09:04 | substr sous linux

juliusius37

bonjour tous le monde
je doit actuellement cree un petit programme en C++
qui joue avec des fichiers des chaine.
ce programme fonctionnera sous linux, mais a la compilation j'ai la message d'erreur suivant:
'substr' undeclared (first use this function)
je ne connais pas trop le c++ et encore moins sous linux
Pourtant je croyais que cette fonction ce trouvé dans string.h comme sous windows. mais est ce différent sous linux??
aidez moi svp
mailto:S0KARISS@hotmail.com

vendredi 13 février 2004 à 14:24:16 | Re : substr sous linux

Hylvenir

substr est une méthode de la classe string
dans le fichier <string>

#include <string>
#include <iostream>
using namespace std;

int main( int, char*[])
{
string ch = "Bonjour";
cout << ch.substr( 1, 3 ) << endl;
}

vendredi 13 février 2004 à 15:38:14 | Re : substr sous linux

juliusius37

merci, j'av trouvé en fait substr est pas une fonction mais une methode, j'av complétement oublié comme je suis pas un crac en c++.
Voila le code de ma fonction.
ce qui ne marche pas c les (strcasecmp(r_in_rec[k].substr(knownField_start[7],len),"C")==0) et les strcmp bien sur le message d'erreur est le suivant

cannot convert 'std::basic_string<char, std::char_traits<char>,std::allocator<char> >' to 'const char*' for argument '1' to 'int strcmp>const char*, const char*)'

je vois bien qu'il y a un pb de type mais je n'ai aucune idée de comment le résoudre
aidez moi svp!!!


void Levelone_Tex::separate_String(vector<string> &r_in_rec, vector<string> &r_data, const int START_REC, const int END_REC) {


#ifdef DEBUG
cout << "\nseparate_String(vector<string> &r_in_rec, vector<string> &r_data, const int START_REC, const int END_REC) called.";
#endif

//If this is not done, the memory will run full with giant strings !!!
data_str=""; // Delete data from last execution by assigning empty string!
outp=""; // Delete data from last execution by assigning empty string!

int Number=999; // for token assignment

/*
This loop goes through the records from given start START_REC
to end END_REC. Within this loop another loop separates the fields.
*/
for(int k=START_REC; k<=END_REC; k++)
{
bool reject_record = false; // For rejection of single records
bool is_special_case = false;

// (!) If this is not done, the data fields of the last record are
// compared again, which is wrong.
for(int i=0; i<i_nr_Token; i++)
{
r_data[i]=""; // Overwrite old data fields with ""
}

#ifdef DEBUGSTRING
cout << "\nFOR-loop of records! -- k: " << k;
#endif

#ifdef DEBUGSTRING
cout << "\n(!)r_in_rec["<< k <<"].length(): " << r_in_rec[k].length();
#endif

/*
In this for-loop the program goes through the fields of each line
(each line is a record) and assigns the corresponding token for each field.
*/
for(int p=0; p<i_nr_knownFields; p++)
{
reject_record = false; // Initial value - record is not ignored
is_special_case = false;

unsigned int len=knownField_end[p]-knownField_start[p]+1; // length of data field

#ifdef DEBUGSTRING
cout << "\nknownField_start[" << p << "]: " << knownField_start[p];
cout << "\nknownField_end[" << p << "]: " << knownField_end[p];
cout << "\nlen: " << len;
#endif

// (!) Look up number in the declaration in assign_token() //
Number = Token_Nr[knownField_start[p]][knownField_end[p]];
/////////////////////////////////////////////////////////////

#ifdef DEBUGSTRING
cout << "\n---TEST! - " << k << " - " << p;
#endif


#ifdef DEBUGSTRING
cout << "\nNumber: " << Number;
#endif

//Length -2 because of Index starting with 0 and linefeed at the end.
int last_pos = r_in_rec[k].length()-2;
// Field position is greater than last field position and token is therefore ignored
if(knownField_start[p] > last_pos)
{
#ifdef DEBUGSTRING
cout << "\nbreak! knownField_start[p] > last_pos";
#endif
break; //for(int p=0; p<i_nr_knownFields; p++) is stopped.
//Because current line has ended.
}

if(Number==999) // indicates special treatment!
{
if(p==14) // Site-ctrl treatment
{
if(strcasecmp(r_in_rec[k].substr(knownField_start[7],len),"C")==0) // if Source_code_1==C
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into ROUTE (SOURCE_CODE_1=C)!";
#endif
// insert value in site_ctrl into ROUTE
r_data[54]=r_in_rec[k].substr(knownField_start[14],len);
}

if(((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"R")==0) ^ ((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"L")==0)) // if Source_code_1==R or L
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into RECEIVER_NAME (SOURCE_CODE_1=R or L)!";
#endif
// insert value in site_ctrl into REICEVER_NAME
r_data[55]=r_in_rec[k].substr(knownField_start[14],len);
}

if((strcmp(r_in_rec[k].substr(knownField_start[7],len),"B"))==0) // if Source_code_1==B
{
if(strcmp(r_in_rec[k].substr(knownField_start[10],len),"99")==0) // if Source_code_2==99
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into RELLABELED_TO_PARCEL_ID (SOURCE_CODE_1=B and SOURCE_CODE_2=99)!";
#endif
// insert value in site_ctrl into RELLABELED_TO_PARCEL_ID
r_data[53]=r_in_rec[k].substr(knownField_start[14],len);
}
else // if Source_code_1==B and Source_code_2!=99
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into COMMENTS (SOURCE_CODE_1=B)!";
#endif
// insert value in site_ctrl into COMMENTS
r_data[56]=r_in_rec[k].substr(knownField_start[14],len);
}
}

if((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"P")==0) // if Source_code_1==P
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into WEIGHT (SOURCE_CODE_1=P)!";
#endif
// insert value in site_ctrl into WEIGHT
r_data[8]=atoi(r_in_rec[k].substr(knownField_start[14],len))*100;
}

if((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"S")==0) // if Source_code_1==S
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Site_ctrl take into COMMENTS (SOURCE_CODE_1=S)!";
#endif
// insert value in site_ctrl into COMMENTS
r_data[56]=r_in_rec[k].substr(knownField_start[14],len);
}

}// End of if(p==14)
if(p==75) //relabeled_to_parcel_id treatment
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Relabeled_to_parcel_id treatment!";
#endif
//Relabeled_to_parcel_id
if((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"Y")==0) //if source_code_1==Y
{
r_data[53]=r_in_rec[k].substr(knownField_start[75],len);
}
}// End of if(p==75)

if(p==33) //Change Volume which was in dm3 in cm3
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Change Volume, which was in dm3, in cm3!";
#endif
// Change Volume which was in dm3 in cm3
r_data[9]=(r_in_rec[k].substr(knownField_start[33],len)) + "000";
}

if(p==9) // Source_code_1 treatment
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Source_code_1 treatment!";
#endif
if((strcmp(r_in_rec[k].substr(knownField_start[7],len)),"E")==0) //if source_code_1==E
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Source_code_1 = E!";
#endif
if(strcmp(r_in_rec[k].substr(knownField_start[7],len),r_in_rec[k].substr(knownField_start[30],len))==0)
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Source_code_1 = E2!";
#endif
// Change E in E2
r_data[5]="E2";
}
else
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Source_code_1 = E1!";
#endif
// Change E in E1
r_data[5]="E1";
}
}// End of if(strcmp(r_in_rec[k].substr(knownField_start[7],len),"E")==0)

}// End of if(p==9)

if(p==31) // Take weight in gram
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Take weight in gram";
#endif
// Take weight in gram
r_data[8]=atoi(r_in_rec[k].substr(knownField_start[31],len)) * 100;
}//End of if(p==31)

if(p==12) // add time to date
{
#ifdef DEBUGSTRING
cout << "\nSPECIAL CASE: Add time to date token!";
#endif
// Add time to date
r_data[7]+=r_in_rec[k].substr(knownField_start[12], len);

}//End of if(p==12)

//cout << "\nis_special_case: " << is_special_case;


if(p==41) // Set option code - token numbers
{ // Read the option code vector from the input file random

vectproduct = r_in_rec[k].substr(knownField_start[41],30); // substr(startpos, length) !

#ifdef DEBUGSTRING
cout << "\nvectproduct: " << vectproduct;
#endif


if(vectproduct.substr(29,1)=="Y") // TATEX
{
#ifdef DEBUGSTRING
cout << "\nExpress trafic";
#endif
}

if(vectproduct.substr(28,1)=="Y") // EUROCOMFORT
{
#ifdef DEBUGSTRING
cout << "\nDeferred trafic";
#endif
}

if(vectproduct.substr(27,1)=="Y") // CHRONO 12
{
r_data[17]="1";
#ifdef DEBUGSTRING
cout << "\nChrono 12";
#endif
}

if(vectproduct.substr(26,1)=="Y") // COMPTANT
{
#ifdef DEBUGSTRING
cout << "\nComptant";
#endif
}

if(vectproduct.substr(25,1)=="Y") // DECLARED VALUE
{
r_data[18]="1";
#ifdef DEBUGSTRING
cout << "\nDeclared value";
#endif
}

if(vectproduct.substr(24,1)=="Y") // RECEIVER PAYS
{
r_data[19]="1";
#ifdef DEBUGSTRING
cout << "\nReceiver pays";
#endif
}

if(vectproduct.substr(23,1)=="Y") // CASH ON DELIVERY
{
r_data[20]="1";
#ifdef DEBUGSTRING
cout << "\nCash on delivery";
#endif
}

if(vectproduct.substr(22,1)=="Y") // NIGHT TRAFIC
{
r_data[21]="1";
#ifdef DEBUGSTRING
cout << "\nExpress 8";
#endif
}

if(vectproduct.substr(21,1)=="Y") // IMPERATIF
{
r_data[22]="1";
#ifdef DEBUGSTRING
cout << "\nExpress 10";
#endif
}

if(vectproduct.substr(20,1)=="Y") // TATEX STANDARD
{
r_data[17]="1";
#ifdef DEBUGSTRING
cout << "\nTatex standard";
#endif
}

if(vectproduct.substr(19,1)=="Y") // SATURDAY DELIVERY
{
r_data[23]="1";
#ifdef DEBUGSTRING
cout << "\nSaturday delivery";
#endif
}

if(vectproduct.substr(18,1)=="Y") // HORS NORME
{
r_data[24]="1";
#ifdef DEBUGSTRING
cout << "\nHors norme";
#endif
}

if(vectproduct.substr(17,1)=="Y") // DANGEROUS GOODS
{
r_data[25]="1";
#ifdef DEBUGSTRING
cout << "\nDangerous goods";
#endif
}

if(vectproduct.substr(16,1)=="Y") // SPEEDER DA
{
#ifdef DEBUGSTRING
cout << "\nSpeeder DA";
#endif
}

if(vectproduct.substr(15,1)=="Y") // RETOUR CHEQUE EXPRESS
{
r_data[26]="1";
#ifdef DEBUGSTRING
cout << "\nCheque express return";
#endif
}

if(vectproduct.substr(14,1)=="Y") // EUROTATEX DD
{
#ifdef DEBUGSTRING
cout << "\nExpress trafic";
#endif
}

if(vectproduct.substr(13,1)=="Y") // EUROTATEX DA
{
r_data[27]="1";
#ifdef DEBUGSTRING
cout << "\nStay in depot";
#endif
}

if(vectproduct.substr(12,1)=="Y") // WORLDTATEX DD
{
#ifdef DEBUGSTRING
cout << "\nWorldtatex dd";
#endif
}

if(vectproduct.substr(11,1)=="Y") // WORLDTATEX DA
{
r_data[27]="1";
#ifdef DEBUGSTRING
cout << "\nStay in depot";
#endif
}

if(vectproduct.substr(10,1)=="Y") // STAY IN DEPOT
{
r_data[27]="1";
#ifdef DEBUGSTRING
cout << "\nStay in depot";
#endif
}

if(vectproduct.substr(9,1)=="Y") // POIDS VOLUME
{
#ifdef DEBUGSTRING
cout << "\nPoids volume";
#endif
}

if(vectproduct.substr(8,1)=="Y") // DIRECTATEX
{
r_data[17]="1";
#ifdef DEBUGSTRING
cout << "\nDIRECTATEX";
#endif
}

if(vectproduct.substr(7,1)=="Y") // OCI
{
r_data[28]="1";
#ifdef DEBUGSTRING
cout << "\nCollect on request";
#endif
}

if(vectproduct.substr(6,1)=="Y") // GRATUIT
{
r_data[29]="1";
#ifdef DEBUGSTRING
cout << "\nFree duty and taxe";
#endif
}

}//End of if(p==41)

is_special_case = true; // data is already set

}//End of if(Number==999) //////////////////////

if(Number==999 && is_special_case==false) // Number is still 999 ==> No special treatment took place.
{
errormsg = "Error - special treatment of data field marked with Number==999 does not take place!";
#ifdef LOG4C
Gtb_a_anom(errormsg, GTB_UM_IDANO, 1, s_name_Application, ifile, s_Param_Scan_Source, s_error_outputfile);
#endif

#ifdef DEBUG
cout << endl << errormsg;
#endif
}


#ifdef DEBUGSTRING
cout << "\nis_special_case: " << is_special_case;
#endif

if(is_special_case ==false) // Only for normal assignments
{
r_data[Number] = r_in_rec[k].substr(knownField_start[p],len);

while( r_data[Number].length() < len) // Keep field length defined in len
{
r_data[Number] = r_data[Number] + " "; // fill end with " "
}

#ifdef DEBUGSTRING
cout << "\ndata[" << Number << "]: " << r_data[Number];
#endif
/////////////////////////////////////////////////////

unsigned int cr_in_data = r_data[Number].find("\r",0); // Check for fields which contain carriage return
#ifdef DEBUGSTRING
cout << "\ncr_in_data: " << cr_in_data;
#endif
if(cr_in_data != string::npos)
{
r_data[Number][cr_in_data] = ' '; // Character is replaced by ' ' (space)
}

unsigned int lf_in_data = r_data[Number].find("\n",0); // Check for fields which contain line feed
#ifdef DEBUGSTRING
cout << "\nlf_in_data: " << lf_in_data;
#endif
if(lf_in_data != string::npos)
{
r_data[Number][lf_in_data] = ' '; // Character is replaced by ' ' (space)
}

//////////////////////////////////////

}//End of if(is_special_case ==false)

}//End of for(int p=0; p<i_nr_knownFields; p++)

if( reject_record == false) // Rejected records are not written into the output file
{

bool is_empty=true; // for field checks
bool only_blanks; // for field checks

int i=0; //Index for data

while(i<i_nr_Token)
{
only_blanks=true; // Set for every execution in while... Until found otherwise
const char *c_data; // pointer on char array
c_data = r_data[i].c_str();
while(*c_data) // Fields which only contain blanks are empty
{
if(*c_data++!=' ')
{
#ifdef DEBUGSTRING
cout << "\n" << "data[" << i << "] contains data - only_blanks = false";
#endif
only_blanks = false;
break;
}
}//End of while(*c_data)
if(only_blanks == true)
{
// All fields except option code fields
if(i<i_first_token_product ^ i>i_last_token_product) // ^ is XOR
{
r_data[i]=""; // Write empty fields as ||
#ifdef DEBUGSTRING
cout << "\n" << "only_blanks == true -- data[" << i << "] = \"\" ";
#endif
}
else
{
// Filling the option code fields which are not 1 with 0.
// This has to be done for every line.
r_data[i]="0";
#ifdef DEBUGSTRING
cout << "\n" << "Option code field data[" << i << "] initialized with 0.";
#endif
}
}// End of if(only_blanks == true)
if(strcmp(r_data[i].c_str(),"")!=0) // if at least one field contains data
is_empty = false;
// don't break, because the rest of the fields should be checked, too!
i++;//Increase i -- check next token data
}//End of while(i<i_nr_Token)


#ifdef DEBUGSTRING
if(is_empty == false)
cout << "\nis_empty == false -- At least one field contains data.";
#endif

if(is_empty == false) // Record contains data
{

data_str = data_str + "\n" + Record_Type_Data + Index_Number_String + s_sep;
int s=0;
while(s<i_nr_Token)
{
if(Token[s]!=s_no_entry) // Only token elements which are not ignored
data_str = data_str + r_data[s] + s_sep;
s++;
}// while(s<i_nr_Token)

#ifdef DEBUGSTRING
cout << "\ndata_str: " << data_str;
#endif

}//End of if(is_empty == false)

}//End of if( reject_record == false)

//cout << "\ndata_str:\n" << data_str;

/*
#ifdef DEBUG
// Time output for performance checks
if(k==remember+100)
{
time(¤tsec);
ftime(&tstruct);
currentmillis = tstruct.millitm;
current_time_millis = currentsec + (double)currentmillis/1000;
time_with_millis = current_time_millis - old_time_millis;

cout << "\nseparate_String - 1st. loop - " << k << " - Current time is: " << time_with_millis << endl;
remember = k;
}
#endif
*/

}//End of for(int k=START_REC; k<=END_REC; k++)

///////////////////////////////////////////////////////
outp = data_str; // Add current data_str to output outp
///////////////////////////////////////////////////////

#ifdef DEBUG
time(¤tsec);
ftime(&tstruct);
currentmillis = tstruct.millitm;
current_time_millis = currentsec + (double)currentmillis/1000;
time_with_millis = current_time_millis - old_time_millis;
cout << "\nseparate_String - end - Current time is: " << time_with_millis << endl;
#endif

}//End of method void Levelone_Tex::separate_String(vector<string> &r_in_rec, vector<string> &r_data, const int START_REC, const int END_REC)


///////////////////////////////////////////////////////////////////////////
mailto:S0KARISS@hotmail.com

vendredi 13 février 2004 à 15:51:45 | Re : substr sous linux

Hylvenir

substr renvoie une string...

utilise substr(...).c_str()

(strcasecmp(r_in_rec[k].substr(knownField_start[7],len).c_str(),"C")==0) et les strcmp bien sur le message d'erreur est le suivant


Mais pourquoi mélanger du C et du C++ ?

lundi 16 février 2004 à 08:48:35 | Re : substr sous linux

juliusius37

Merci pr le c_str()
en fait je part d'un code existant que je transforme et les
#ifdef si c'est bien cela qui est issue du C Servent au messages d'erreurs, et comme il y en a plein et qu'il marche en C++ je prefere ne pas les changer, en + je ne suis qu'en stage dans l'entreprise dc je fait ce que l'on me dit, surtout que le c++ et le c je ne connaissais pas il y a encore 2 semaine, j'av juste quelques petites notions.
mailto:S0KARISS@hotmail.com



Cette discussion est classé dans : programme, url, linux, substr


Répondre à ce message

Sujets en rapport avec ce message

Executables Win et Linux [ par thebigbang ] bonjour à tous,Je me demandais s'il était possible de créer des executables avec C++ qui tourne à la fois sous windows et sous Linux ...Je vous remerc portage Linux vers visual C++ [ par acsx ] Salut,J'ai un programme écrit en C sous Linux et je souhaite le faire tourner sous Visual C++ 6.0 sous Windows.J'ai donc créé un projet win32 et intég compilation c++ sous linux [ par marco2009 ] voila je commence a programmer en cpp et lorsque j'essaye de compile avec la console en utilisant gcc ca marche pour les programme ecrit en c mais po Aidez moi programme sur des liens hypertexes et ... [ par borower ] BonjoursJe suis un debutant et je n'arrive pas a mettre en forme un code convenable pour un programme.Je recherche le code source d'un programme en C Equivalant de getch() pour Linux [ par Siriusgalaxie26 ] J'aimerai savoir quelle est la (ou les) fonction(s) équivalente(s) à getch() (de conio.h pour Win) pour les systemes UNIX tel que Linux?Merci S</font aide sur visual c++ svp [ par morgandetoi06 ] alors deja bonjour ;)j aimerait savoir comment faire pour que la console d un programme ms-dos compilé par visual c++ ne se voit pas.pour dev c++ j al programme graphique sous linux avec X11 de préférence (pour le portage) [ par coucou747 ] je recherche une façon de créer des programmes graphiques en c, j'ai cherché des heures sur le net des programmes en c utilisant des librairies comme Chemin actuel [ par victorcoasne ] Comment savoir l'emplace où est le programme en execution+Clair : Je veux un code qu'on met dans un programme et qui me dis qd le prog est executé où linux compatible windows ? [ par Krox68 ] je suis sous linux depuis quelques semaines et j'amerais savoir si on peut faire un prog LINUX compatible WINDOWS (sans Wine)Romano <a href="mailto:ro Linux [ par victorcoasne ] Bonjour j'ai mis Linux en Multi-boot et avec leur environnement de dévelopement C++ , j'arrive pas à complier !!Comment faites-vous ?Vous voulez des R


Nos sponsors

Sondage...

CalendriCode

Décembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

Téléchargements

Logiciels à télécharger sur le même thème :



Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel BAÏSE, Merci à Vincent pour ses précieux conseils
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés
Temps d'éxécution de la page : 0,359 sec

Google Coop CodeS-SourceS Google Coop CodeS-SourceS


Certaines images présentes sur le site (notament certains avatars) sont issues des collections IconShock, donc si vous souhaitez utiliser ces icons vous devez les acheter, ne les copiez pas et ne utilisez pas dans vos sites et applications sans les avoir commandé.