C ++: const char * to stringstream

Is it possible to put const char * in a string stream?

I think this is possible with the write () function in stringstream, but it's hard for me to figure out how to get streamsize if I only know about const char *.

Assuming the size of my const char * constant:

stringstream s; s.write(temp,size); 

How do i get the size? Thanks.

+4
source share
3 answers

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 
+9
source

I don't know if this is the best way to answer your question, but look:

 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 the "flag" will save two "\ 0" more ... I do not know what you have. But I hope that it will be useful for you ... Maybe someone wants to fix it ...

+1
source

You may be misunderstood, but I think you are asking how to get the char* string size. To do this, you need strlen(str) , which is in the <cstring> header. The line must be terminated by zero.

0
source

Source: https://habr.com/ru/post/1342213/


All Articles