C++ map of a key and file doesn't work [duplicate] - c++

This question already has answers here:
std::map<>::insert using non-copyable objects and uniform initialization
(2 answers)
Handling map of files in c++
(1 answer)
Closed 5 years ago.
I want to create a map of a key and the corresponding file in C++. I used the below snipper, which is giving mean exception at last line *m_jsTabFilesMap[key] << text;:
std::map<std::string, std::ofstream*> m_jsTabFilesMap;
std::string text = "hello all";
std::string key = "a";
*m_jsTabFilesMap[key] << text;

The copy constructor of std::ofstream is explicitely deleted. You have to store the streams by pointer or in a different way that doesn't try to copy the stream object if you want them in the map.

Related

How can I use std::string for Const Char Parameter [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 2 years ago.
I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded filepath, like this std::rename(filepath, destination);?
I don't know how you want to populate the strings in question, but here you go:
std::string fromName {"whatever you're going to do"};
std::string toName {"whatever you're going to do"};
std::rename(fromName.c_str(), toName.c_str());

C++ adding n strings to a string stream [duplicate]

This question already has answers here:
Variable number of arguments in C++?
(17 answers)
Closed 4 years ago.
I am kind of stuck in pre C++ 11 land. How can I write a function that takes n strings and appends them to an ostreamstream?
void Foo(std::string first_part, ...){
std::ostringstream oss;
oss << first_part << ...; // cant do it
for(int i = 0; i < ....length(); i++){ // :|
}
}
If I lived in a perfect world I could do the above. Is there any other way pre C++ 11 to loop through the ... arguments?
Sorry, but it can't be done directly (at least not in portable code).
Attempting to pass a non-trivial type (including std::string) as a variadic argument gives undefined behavior.
If you want to do something similar, you could (for one example) pass the addresses of a number of strings rather than attempting to pass the strings themselves.
If you do that, you'll still have to contend with one other detail: you'll need to tell the receiving function the number of (addresses of) strings to expect.
From there, the receiving function would use va_start, va_arg and va_end to retrieve the data and do it's thing with them.

(C++) How to convert a string into a character vector? [duplicate]

This question already has answers here:
How to copy std::string into std::vector<char>? [duplicate]
(2 answers)
Closed 7 years ago.
How to convert a string such as "Hello, world!" into a vector of characters? I've seen many techniques on how to do this for plain arrays, but none for vectors. Here is my code:
string raw_text = "Hello, world!";
vector<char> char_text;
Any way to make a character vector from raw_text?
Thanks in advance!
vector<char> char_text(raw_text.begin(), raw_text.end());

Is it possible to declare a variable as a string? [duplicate]

This question already has answers here:
Convert string to variable name or variable type
(7 answers)
Closed 7 years ago.
So basically what I'm trying to find out, take this scenario:
std::string input;
Ask user for a string?: Apples
cin >> input;
std::string Apples = "input";
So basically, I ask the user for a string, and then create a variable with name of that string. Is this possible?
No, you can't do that.
The closest functionality is to use a map.
std::map<std::string, int> aMap;

MoveFileA() doesn't like my arguments [duplicate]

This question already has answers here:
How to convert std::string to LPCSTR?
(9 answers)
Closed 8 years ago.
I have a list of file names in a .txt document, and I would like to move each of these files from one folder to another.
Using MoveFileA() I am getting the error, "no suitable conversion between std::string and LCPSTR".
Here is my code, after opening up my .txt file:
while (std::getline(myfile, line))
{
std::string oldLocation = "C:\\Users\\name\\Desktop\\docs\\folder1\\" + line;
std::string newLocation = "C:\\Users\\name\\Desktop\\docs\\folder2\\" + line;
MoveFileA(oldLocation, newLocation);
}
If I type in the full path as arguments for MoveFileA, instead of sending it a variable, it works but I am unable to iterate over .txt file this way.
Any suggestions on how I might fix this?
LCPSTR means long constant pointer to a string, which means it's a null terminated c string.
std::string is an object. It is something different. But it luckily provides a convenience method c_str the provides a pointer to a constant c style string. So as the comment says you should go by:
MoveFileA(oldLocation.c_str(), newLocation.c_str());
It is worth of explicitly noting, that you can't drop it in every place instead of char*, but only when the string won't be modified. It returns const char*. This is where the C in LCPSTR gets important.