Portál AbcLinuxu, 11. května 2025 04:33
Řešení dotazu:
ostream& strm; char mojepole[] = {0,1,0,2}; strm.write(mojepole, sizeof(mojepole));A operátor <<, který výstupy formátuje (tam to asi nefunguje? protože se provede decay na char* a tam se očekává nulou ukončený string?), bych v tomto případě vůbec nepoužíval. Protože zapisuješ binární data, nejedná se o čitelný textový soubor, proto je veškeré formátování a s tím spojené problémy (závislost na bindnutém locale) blbost řešit.
#include <iostream> #include <sstream> using namespace std; int main() { ostringstream strm; char mojepole[] = {0, 1, 0, 2}; strm.write(mojepole, sizeof(mojepole)); string s = strm.str(); cout << "string length: " << s.size() << endl; for (char c : s) { cout << int(c) << ", "; } cout << endl; return 0; }Výstup je
string length: 4 0, 1, 0, 2,
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.