screen print an unsigned char array with iostream

3

I have a console application in C ++ in Visual Studio Express 2013 with the following code:

#include <iostream>
#include <ostream>
using namespace std;

void GetValores(unsigned char* c)
{
    // aquí va mucho código que asigna valores a otro puntero unsigned char llamado temp

    int i = 0;
    while (*temp != '
string str;
str.assign((char*)cadena, 256);  // los valores se copian bien
cout << str;
') // copio los valores { c[i] = *temp; temp++; i++; } c[i] = '
std::ostream << str;
'; // le pongo el caracter nulo al final. } int _tmain(int argc, _TCHAR* argv[]) { unsigned char* cadena = new unsigned char[256]; GetValores(cadena); // todo bien, recibo mis valores como debe ser }

My problem is that now I want to print the values of cadena on the screen. I have tried with:

#include <iostream>
#include <ostream>
using namespace std;

void GetValores(unsigned char* c)
{
    // aquí va mucho código que asigna valores a otro puntero unsigned char llamado temp

    int i = 0;
    while (*temp != '
string str;
str.assign((char*)cadena, 256);  // los valores se copian bien
cout << str;
') // copio los valores { c[i] = *temp; temp++; i++; } c[i] = '
std::ostream << str;
'; // le pongo el caracter nulo al final. } int _tmain(int argc, _TCHAR* argv[]) { unsigned char* cadena = new unsigned char[256]; GetValores(cadena); // todo bien, recibo mis valores como debe ser }

but on line cout << str; the compiler throws me the error: "no operator matches these commands, the operand types are std :: ostream <

but when I change it for:

%pre%

now the compiler tells me "an identifier was expected", and it will not let me print my string.

    
asked by Broken_Window 21.09.2016 в 18:09
source

1 answer

3

The GetValores function should not be named that way, since get means get , and in the code you are assigning values, then you should name setValores , with lowercase because it is not a data structure struct .

It is not necessary to create a function that works like a factory of strings in C ++ because they are assigned automatically, so GetValores would be discarded.

The only thing that should be put inside the main is this, nobody goes through life creating unsigned char .

string str = "Hola Mundo";
cout << str << endl;

As you say @PaperBirdMaster, just in case, include string , it should look like this.

#include <iostream>
#include <ostream>
#include <string>
    
answered by 21.09.2016 / 19:08
source