How to create C++ istringstream from a char array with null(0) characters?
By : AX5
Date : March 29 2020, 07:55 AM
around this issue I have a char array which contains null characters at random locations. I tried to create an iStringStream using this array (encodedData_arr) as below, , There is nothing special about null characters in strings code :
std::istringstream iss(std::string(data, N));
setBlob(&iss);
std::istringstream iss("haha a null: \0");
std::istrstream iss(data, N);
struct myrawr : std::streambuf {
myrawr(char const *s, size_t n) {
setg(const_cast<char*>(s),
const_cast<char*>(s),
const_cast<char*>(s + n));
}
};
struct hasb {
hasb(char const *s, size_t n)
:m(s, n)
{ }
myrawr m;
};
// using base-from-member idiom
struct myrawrs : private hasb, std::istream {
myrawrs(char const *s, size_t n)
:hasb(s, n),
std::istream(&static_cast<hasb*>(this)->m)
{ }
};
|
How to initialise std::istringstream from const unsigned char* without cast or copy?
By : user3317007
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Your answer is still copying. Have you considered something like this? code :
const unsigned char *p;
size_t len;
std::istringstream str;
str.rdbuf()->pubsetbuf(
reinterpret_cast<char*>(const_cast<unsigned char*>(p)), len);
|
Read lines from istringstream ,without '\r' char at the end
By : Sarika
Date : March 29 2020, 07:55 AM
Does that help I have a following function : , You can remove the '\r' from the end using this code: code :
if(line[line.length() - 1] == '\r') line = line.substr(0, line.length() - 1);
std::istream& univGetline(std::istream& stream, std::string& line)
{
std::getline(stream, line);
if(line[line.length() - 1] == '\r') line = line.substr(0, line.length() - 1);
return stream;
}
void process (std::string str)
{
std::istringstream istream(str);
std::string line;
std::string specialStr("; -------- Special --------------------");
while (univGetline(istream,line))
{
if (strcmp(specialStr.c_str(), line.c_str()) != 0)
{
continue;
}
else
{
//special processing
}
}
}
|
Is there a way I can read two char from the istringstream?
By : daidailanlan
Date : March 29 2020, 07:55 AM
I wish this helpful for you I expect the output of 78 5 + to be 83. However I am getting 13. code :
int postFixEval(string str)
{
istringstream in = istringstream(str);
stack<int> postFixStack;
skipWhiteSpace(in);
while (in)
{
char ch = in.peek();
if (isdigit(ch)) {
int num;
in >> num;
postFixStack.push(num);
}
else {
char op = in.get();
if (op == '+') {
int num1 = postFixStack.top();
postFixStack.pop();
int num2 = postFixStack.top();
postFixStack.pop();
postFixStack.push(num1 + num2);
}
}
}
return postFixStack.top();
}
|
How to convert istringstream object to string and char array?
By : austin
Date : March 29 2020, 07:55 AM
This might help you I have this object: , For a std::string: ob_stream.str();
|