'What is C translation for Matlab‘s `fread()` function?

I want to translate a MATLAB code to C, but the fread() function of MATLAB confused me a lot, I don't know how to translate it properly. I have used recv() function to read the data, but it's not the same as what I got in MATLAB.

MATLAB code is here:

field_len = fread(t, 1, 'uint16');        % t is a TCP object
code = fread(t,1);
bytes = fread(t, field_len-1, 'uint8');   % field_len > 1

My C code is here(Ignore the code that establishes the TCP link):

uint16_t field_len = 0;
uint8_t code = 0;
uint8_t bytes[field_len-1];

recv(new_server_socket, &field_len, sizeof(field_len), 0);  // new_server_socket is server socket to get data
recv(new_server_socket, &code, sizeof(code), 0);
recv(new_server_socket, bytes, sizeof(bytes), 0);

An numeric example. In MATLAB, field_len should be 213. But in my C code, it is 54528. I don't know what is the problem.

Any insights would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source