std string to char
-
Hallo zusammen,
ich möchte hf zusammensetzen, leider bekommme ich dieses nicht hin. Für eine Hilfestellung wäre ich sehr dankbar. Der folgende Code stellt dabei ein Gedankenmodell dar.
#include <string> using namespace std; const long refpropcharlength=255; const long ncmax=20; int main() { char hf[refpropcharlength*ncmax]; string FLD[20]; FLD[1] = "bla1"; FLD[2] = "|bla2"; FLD[3] = "|bla3"; hf = FLD[1]+FLD[2]+FLD[3]; return 0; }
Gruss++
Masl
-
Also ich hab mal bisserl rumgetüftelt, ist vieleicht nicht elegant aber funktioniert.
#include <iostream> #include <string> #include <cstring> const int refpropcharlength = 511; const int ncmax = 20; int main(int argc, char* argv[]) { char hf[refpropcharlength*ncmax]; std::string fld[20]; for(unsigned int a = 0; a < 20; a++) { fld[a] = "Hallo "; } std::string output; for(unsigned int a = 0; a < 20; a++) { output += fld[a]; } unsigned int b; for(b = 0; b < strlen(output.c_str()); b++) { hf[b] = output[b]; } hf[b] = '\0'; std::cout << hf << std::endl; return 0; }
-
Stimmt ist nicht sonderlich elegant.
Dafür gibt in der Stringbibliothek der STLstring::c_str string::append string::push_back operator+
Und das ganze kann dann so ausehen:
#include <iostream> #include <cstring> #include <string> using namespace std; int main() { char hf[500]; string sack; string FLD[20]; FLD[1] = "bla1"; FLD[2] = "|bla2"; FLD[3] = "|bla3"; sack = FLD[1]+FLD[2]+FLD[3]; strcpy(hf, sack.c_str()); cout << hf << " = " << FLD[1]<< " + " << FLD[2]<<" + " <<FLD[3]; return 0; }
Was hat das mit der MFC zu tun?
Tip: Doku lesen.
-
Vielen Dank fur Eure Hilfe!
Gruß
Masl