How do you read from a memory buffer c++

16,106

There is no reason you cannot use a std::string or CString to manipulate that data. (Use higher level constructs when they are available to you.)

To get the data into a std::string, use the constructor or assignment operator:

std::string s( buffer.data, buffer.size );

You can even stick it in a std::stringstream so you can treat the data buffer like a file:

std::istringstream ss( s );

int n;
ss >> n;

Things work similarly for the MFC string class.

To get the data from a string, you'll need to copy it over. Ideally, you'll be able to allocate the data's memory. Assuming you have data written into a stringstream

std::ostringstream ss;
ss << name << "," << employee_number;

You can then allocate the space you need using the function that creates the data_buffer object:

function_that_creates_a_data_buffer( buffer, ss.str().size() );

If there is no such function (there ought to be!) you must malloc() or new it yourself, as appropriate:

buffer.size = ss.str().size();
buffer.data = (char*)malloc( buffer.size );

Now just copy it:

ss.str().copy( buffer.data, buffer.size );

If your buffer needs a null-terminator (I have so far assumed it doesn't), make sure to add one to the size you allocate and set the last character to zero.

buffer.size = ss.str().size + 1;
buffer.data = new char[ buffer.size ];
ss.str().copy( buffer.data, buffer.size );
buffer.data[ buffer.size-1 ] = 0;

Make sure to look at the documentation for the various classes you will use.

Hope this helps.

Share:
16,106
fudge22it
Author by

fudge22it

I am currently a college student studying computer science. I am working at a local software development company doing testing and some development. I just barely started learning website development.

Updated on June 04, 2022

Comments

  • fudge22it
    fudge22it almost 2 years

    I am fairly new at C++ and am trying to understand how memory manipulation works. I am used to Java and Python and haven't really been exposed to this.

    I am working on a project that has the following structure that doesn't quite make sense to me.

    typedef struct
    {
      size_t size;
      char *data;
    } data_buffer;
    

    This structure basically acts as a buffer, with a pointer to the data stored within the buffer and the size of the buffer to allow the program to know how large the buffer is when reading from it.

    An example of how the program uses the buffer:

    data_buffer buffer = {0};
    //Manipulate data here so it contains pertinent information
    CFile oFile;
    oFile.Write(buffer.data, buffer.size);
    

    The program mostly uses 3rd party code to read the data found within the buffer, so I am having trouble finding an example of how this is done. My main question is how do I read the contents of the buffer, given only a pointer to a character and a size? However, I would also like to understand how this actually works. From what I understand, memory is written to, with a pointer to where it starts and the size of the memory, so I should be able to just iterate through the memory locations, grabbing each character from memory and tagging it onto whatever structure I choose to use, like a CString or a string. Yet, I don't understand how to iterate through memory. Can someone help me understand this better? Thanks.