char konstante verbinden
-
hi,
und zwar möchte ich zwei konstante verbinden.
beispiel:glob_vars.h const char dir[] = "/mein/homedir"; const char file[] = "/test.txt"; // ----------------- main #include "glob_vars.h" ... // eine_funktion( const char*)
die im main benannte funktion soll nun "/mein/homedir/test.txt" als argument bekommen, nur wie mach ich dies am besten?
in perl würde ich einfach meine_funktion("file") schreiben nur bei c++ geht das ja nicht.
mir ist die string bibliothek zwar bekannt, womit ich ohne weiteres ja strings verbinden kann, aber es muss doch noch ne einfachere methode ohne diese geben?cu...
daniel
-
Hallo
benutzt c++
#include <string> const std::string dir = "\\mein\\homedir\\"; const std::string file = "test.txt"; std::string result = dir + file;
bis bald
akari
-
Ansonsten lohnt sich ein Blick auf www.boost.org/libs/format/.
-
danke für die antworten, aber ich dachte eher an was ohne zusätzliche libraries.
-
stdin schrieb:
glob_vars.h const char dir[] = "/mein/homedir"; const char file[] = "/test.txt"; // ----------------- main #include "glob_vars.h" ... // eine_funktion( const char*)
die im main benannte funktion soll nun "/mein/homedir/test.txt" als argument bekommen, nur wie mach ich dies am besten?
die stings sind statisch, gell!
glob_vars.h
#define DIR "/mein/homedir" #define FILE "/test.txt";
main.cpp
//void eine_funktion(char const* fileName); //oder //coid eine_funktion(char* fileName); eine_funktion(DIR FILE);
achtung! das ist kein witz. es klappt!
da biste aber nicht gerade extrem flexibel. die strings müssen compilezeitkonstant sein.
oder nimm moderne strings
glob_vars.h#include <string> std::string const dir="/mein/homedir"; std::string const file="/test.txt";
main.cpp
//void eine_funktion(string const& filename); //oder //void eine_funktion(string filename); eine_funktion(dir+file);
es gibt noch ca 7 wichtige andere varianten, wenn man strcat udn so verwenden will.
vielleicht eine noch sehr wichtig, wenn man an eine_funktion nix ändern darf.glob_vars.h
#include <string> std::string const dir="/mein/homedir"; std::string const file="/test.txt";
main.cpp
//void eine_funktion(char const* filename); string completePath=dir+file; eine_funktion(completePath.c_str());