I'm trying to append my path and contain a variable as part of the path but I'm getting an error.
What's wrong with it?
fstream fin("E:\\Games\\maps\\" + this->MapNumber + ".map", ios::in|ios::binary|ios::ate);
this->MapNumber is a USHORT
error: 13 IntelliSense: expression must have integral or unscoped enum type
In C++ you can't use + to concatenate literal strings. You can use + with std::strings to concatenate them, but that won't work with integer or other types. You need to use a stream instead. Insertion and extraction into a stream will cause the types that support it to represent themselves as text, but you probably already knew this from general I/O.
Try with something like this:
std::stringstream filename;
filename << "E:\\Games\\maps\\" << this->MapNumber << ".map";
std::fstream fin(filename.str().c_str(), ios::in|ios::binary|ios::ate);
Just like with everything else, to use something you need to include the header that declares it first. In order to use std::stringstream you need to include <sstream>.
You can't use operator+ on a string and another type like string or so you can either:
Option1: turn all variables into strings to append them
string s = string("E:\\Games\\maps\\") + string(itoa(this->MapNumber)) + string(".map");
option2: use stringstream as #k-ballo explained
option3: use the good old C fprintf (my personal favourite)
char str[100];
fprintf(str, "E:\\Games\\maps\\ %d .map", this->MapNumber);
Related
I've created a #define which points to a particular directory. I would then like to use this definition in combination with a string literal:
#define PATH_RESOURCES "/path/to/resources/"
std::ifstream datafile(PATH_RESOURCES + "textures.dat");
However, the compiler complains about adding char types using the + operator:
error: invalid operands of types ‘const char [11]’ and ‘const char [13]’ to binary ‘operator+’
So how can I combine a #define with a string literal? Or, is there a better way of doing this altogether? I imagine using a const variable would be an alternative, but this would mean having to pass around yet another parameter which I'd rather prefer to keep as a global definition.
You can combine two string literals by writing them one after the other with no + plus between them:
std::ifstream datafile(PATH_RESOURCES "textures.dat");
The fact that one of the string literals happens to be defined through the preprocessor does not change much: you can do it like this as well:
std::ifstream datafile(PATH_"/path/to/resources/" "textures.dat");
Here is a demo on ideone.
Try
std::ifstream datafile(PATH_RESOURCES "textures.dat");
Two string literals adjacent concatenate.
Use std::ifstream datafile(PATH_RESOURCES "textures.data");
Note the lack of the + operator.
You could also do
std::ifstream datafile(std::string(PATH_RESOURCES) + std::string("textures.data")); if you really wanted.
Create a std::string, assign your #define string to it and add the second literal. Afterwards use the string.
std::string str(PATH_RESOURCES);
str = str + "textures.dat";
std::ifstream datafile(str);
I'm trying to write an mpi program where each node knows its own rank, which is an integer.
I this program I hope each node to create a .txt file with its rank as the file name. That is, I hope the program to generate a file called rank.txt where rank is an integer.
I know how to convert an int to string, but I am quite confused about how I can combine that string with .txt and put it into a filename. What is the easiest way to do it?
Thanks in advance.
EDIT
I have combined the number with .txt and put them into a string filename but when I typed std::ofstream out_stream(filename) the compiler tells me that
no matching constructor for initialization `std::outstream`
How can I put the string into a filename?
The problem is that there's no constructor for std::ofstream that takes a std::string. You need to pass const char*, so just say:
std::ofstream out_stream(filename.c_str());
Things have changed with the latest standard revision, though, so check your compiler documentation for how to enable C++11.
If you've already converted the int to an std::string, say rank_s, then rank_s + ".txt" should be enough.
use fstream and the << operator.
For string concatenation:
std::stringstream vIndex;
vIndex << i;
std::string index = vIndex.str() + ".txt";
If you can use boost:
std::string filename = boost::lexical_cast<std::string>(rank) + ".txt";
Otherwise (for example):
std::ostringstream s;
s << rank;
std::string filename = s.str() + ".txt";
std::string filename = std::to_string(i) + ".txt";
Needs an up to date compiler that supports c++11 for this
edited to answer question :-
If you have c++11 just do
std::ofstream out_stream(std::to_string(i) + ".txt");
If you don't then you're best looking at the other answers
When should I use stringstream instead of string::append()?
Supposing I'm going to catenate just strings.
stringstream ss;
ss << str1 << "str2" << ...
Write(ss.str());
Or:
string str;
str.reserve(10000);
str.append(str1);
str.append("str2");
...
Write(str);
Which of them is faster?
I don't know which one will be faster, but if I had to guess I'd say your second example is, especially since you've called the reserve member function to allocate a large space for expansion.
If you're only concatenating strings use string::append (or string::operator+=).
If you're going to convert numbers to their string representation, as well as format them during conversion, and then append the conversion results together, use stringstreams. I mention the formatting part explicitly because if you do not require formatting C++11 offers std::to_string which can be used to convert numeric types to strings.
string.append is much faster. Especially when you reserve.
If you are concatenating only strings, I would use string.append. I would only use stringstream when I need to automatically convert non-strings to strings for example:
const int x(42);
stringstream ss;
ss << "My favorite number is: " << x << std::endl;
Here stringstream automatically converts x to a string and appends it. I do not need to call atoi. Stringstream will convert all the basic types automatically for you. It is great for that purpose.
Also if you are only going to be directing data into the stringstream to convert it to a string later. You can use ostringstream which is for output.
I hope that helps.
I need to concatenate the parameters in the write function. How would I do this?
Example: ofstream write(newFolder concat "/newfile.txt");
mkdir(newFolder);
ofstream write (wantTo "concat here");
write << "Sup mo fo";
write.close();
If newFolder is a std::string, you can simply use newFolder + "/newfile.txt". You'd have to make sure newFolder does not end with a / or a \ though. You may need to use the c_str() function on your std::string if you require a char* in your write function.
While using std::string is the preferred way, be careful because older versions of the standard library doesn't support an open method that takes a std::string as argument.
If you get an error in the call to open when using std::string, then you have to do it in two steps:
std::string newFolder = "/path/to/folder";
mkdir(newFolder.c_str(), 0644); // The 'mkdir' function wants 'const char *'
// Has to concatenate strings here if your standard library is too old
std::string folderAndFile = newFilder + "/filename";
std::ofstream output(folderAndfile.c_str());
Hello for all people...
Sorry for my english, but speak spanish...
In this week, study and work for this proyect, I want create a software to make files(.us)...
Example
char name[50]; //Or string
cin>>name;
ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name+"\\Classes\\_PlayerPawn.us");
But the compiler has error in the Operator binary plus
Any alternative, examples or something for create the file in specific directory
Good bye and Thx!
Either side of operator+ must be a std::string1 for operator+ to concatenate strings:
string name;
cin >> name;
ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name + "\\Classes\\_PlayerPawn.us");
And use std::string for this stuff; with std::string there's no danger of buffer overflows that you get with char*.
1 Actually it just needs to be a class type that supports operator+, not specifically std::string, but then you have no idea what it will do.
I believe you want name to be a std::string - otherwise, name + [suffix] will try to add the suffix string to the array and will not compile. If you really want to keep the name as an array, you should use strcat to append the strings together.