This question already has answers here:
How do I read an entire file into a std::string in C++?
(23 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I want to read the contents of a file into a string.
string contents(size, '\0'); -> size was determined above using the file.seekg and file.tellg.
file.read((char*) contents.data(), size);
Now, I know that the contents of the string will be overwritten in file.read, so there's no need to initialize the the string to null characters.
Is there a way to do so?
You can do this:
std::string contents(std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{});
But it may not be faster. Either way the initialization is likely to be very fast in comparison to reading from the drive.
Related
This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();
This question already has answers here:
What does the C++ standard state the size of int, long type to be?
(24 answers)
Getting the actual length of a UTF-8 encoded std::string?
(11 answers)
Closed 2 years ago.
I was reading the documentation of string::length and, as you can see, it says that
Returns the length of the string, in terms of bytes.
So my question is, is this ensured to be also the number of char that it contains?
I know that usually a char is 1 byte, but is this ensured somewhere? Like in the standard or somewhere else?
This question already has answers here:
How do I concatenate two strings in C?
(12 answers)
How to concatenate two strings in C++?
(8 answers)
Closed 6 years ago.
I'm trying to get a path (appdata) and append a filename (smss.dll), and combine these two to form C:\users\username\appdata\roaming\smss.dll
I have this already:
static char appdata[MAX_PATH+1];
SHGetSpecialFolderPathA(HWND_DESKTOP, appdata, CSIDL_APPDATA, FALSE);
LPCSTR target = "smss.dll";
How can I combine these to into one variable?
You can use PathAppend (limited to MAX_PATH characters), or PathCchAppendEx starting with Windows 8.
static char appdata[MAX_PATH+1];
SHGetSpecialFolderPathA(HWND_DESKTOP, appdata, CSIDL_APPDATA, FALSE);
strcat(appdata, "\\smss.dll");
The strcat function appends the right-hand string to the left-hand buffer. It assumes the destination has room for the concatenation to be done. If there is not enough space, it is undefined behavior. Thus it is a good idea to check that there is enough space.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the best way to slurp a file into a std::string in c++?
I'm trying to imitate PHP's file_get_contents() function for C++.
However, when I convert a char array into a string, it stops at nullbyte:
fread(charbuf, 1, file_size, fp);
string str(charbuf);
How can I initialize the string as a static size array, and read the file contents directly to that container? Also, how do I check the errors for it, for example if there is not enough memory for initializing that string. This would also get me rid of the temporary memory allocation I'm currently using, which I would like to get rid of.
How about safety? Is it possible that many processes read the same file at same time and/or one of them writes in it at same time when I am reading it? How do I avoid such things happening?
I hope you can answer other way than "string isn't binary container".
I ask to reopen this question for the fact: "Apparently, this question is as relevant as ever: two years later, the two most efficient solutions still copy the whole file contents in memory, and this copy cannot be elided by the optimizer. This is a quite unsatisfactory state of affairs. – Konrad Rudolph Oct 25 '10 at 6:25" at What is the best way to read an entire file into a std::string in C++? Or do you want me to create new question that asks to read a file without having extra copy of the string?
std::ifstream fin("somefile.txt");
std::stringstream buffer;
buffer << fin.rdbuf();
std::string result = buffer.str();
This snippet will put all your file into std::string
I hope you can answer other way than "string isn't binary container.
std::string is a binary container, but the constructor you chose takes a C-style string as an argument. Try a different constructor:
std::fread(charbuf, 1, file_size, fp);
std::string str(charbuf, file_size);
EDIT: Taking into account requirement to avoid memory allocations:
std::string str(file_size, 0);
std::fread(&str[0], 1, file_size, fp);
This question already has answers here:
mfc copy certain sections of a CString
(2 answers)
Closed 7 years ago.
I got a CString str
I want to copy str[3]~ str[5] to a brand new one.
I tried C++ char* 's method, not compatible.
So what is the right way do this in VC++
Thank you,
Have you tried using the the MID function?
str.Mid( 3, 3 ) should give you the substring you are looking for.
Updated reference: http://msdn.microsoft.com/en-us/library/b4c90k3d(v=vs.80).aspx