if I create a string like
string filepath;
how can I change it's variable, blah, blah, blah. An example would be better. For example, for cout it would be
cout<<"users/"<<Name<<".txt";
how do I get the same thing into the variable of filepath??
Printable View
if I create a string like
string filepath;
how can I change it's variable, blah, blah, blah. An example would be better. For example, for cout it would be
cout<<"users/"<<Name<<".txt";
how do I get the same thing into the variable of filepath??
what? Say that again, maybe with complete sentences this time.
Look into std::stringstream and its variants (std::ostringstream if you're only using the various writing functions, std::istringstream if you're only using the reading functions).
Code:#include <sstream>
...
std::ostringstream sstr;
sstr << "users/"<<Name<<".txt";
std::string filepath = sstr.str();
Hi,
You can do something like this as well:
That's called 'concatenating' strings, but you can also just call it "adding" strings together, and most people will understand what you mean.Code:string filename = "";
string Name = "myfile";
filename = "users/" + Name + ".txt";
cout<< filename<<endl;
A variable stores values. In this line:Quote:
how can I change it's variable
how do I get the same thing into the variable of filepath??
string Name = "myfile";
Name is a variable, and it's value is "myfile". So, you would say, "how do I assign the variable Name a value". Or, "how can I change the variable's value?". Assignment is what you do when you use the equals sign(=).
Good point. If you're patching only strings together, then stringstream is a bit overkill. If you ever intend to mix in ints and other common datatypes though, that you might use with cout, then you'd probably like to use a stringstream.