Is it possible to put a const char* into a string stream?
I think it's possible with the write() function in stringstream but I'm having trouble figuring out how to get the streamsize if i only know of the const char *.
Assuming size is my const char * variable:
stringstream s;
s.write(temp,size);
How do I get size? Thanks.
I tested it, and it works correctly...
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s;
const char* token = "HELLO";
s << token;
cout << s.str() << endl;
return 0;
}
[facu@arch ~]$ g++ p.cpp
[facu@arch ~]$ ./a.out
HELLO
I may not be understanding correctly, but I think you're asking how to get the size of a char*
string. To do that, all you need is strlen(str)
which is in the <cstring>
header. The string must be null terminated, though.
I don't know if it is the best way to answer your question, but see:
std::string _getline_ ( const char *str, const unsigned int ui_size )
{
std::string tmp;
unsigned short int flag = 0;
while ( flag < 2 )
{
if( *str != '\0' )
{
tmp.push_back( *str );
flag = 0;
}
else
{
tmp.push_back( ' ' );
flag++;
}
str++;
}
return tmp;
}
The problem is that "flag" will store two of "\\0" no more... I don't know what is your proposite.. But i hope it will be useful for you... Maybe someone wanna fix it...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.