fread() in MATLAB and C

2 views (last 30 days)
Pravitha A
Pravitha A on 14 Feb 2020
Commented: Pravitha A on 14 Feb 2020
Is fread() in MATLAB and C different? I have a command
offset = fread(fd, 1, 'uint32');
Can I directly use this comman in a C program. WOuld it give the same result??

Accepted Answer

Walter Roberson
Walter Roberson on 14 Feb 2020
Is fread() in MATLAB and C different?
Yes
Can I directly use this comman in a C program. WOuld it give the same result??
No, it would be an error in multiple ways.
In C, the first parameter would need to be the address of the buffer to put the data into, cast to (void *). The second parameter would be the size of each element in bytes, which for uint32 would be 4 (that is, 4 bytes.) The third parameter would be the count of the number of items to read, which would be 1 for your example.
The 4th parameter needs to be the stream pointer that resulted from an fopen() (or one of some more obscure library calls). Stream pointers in C and C++ are not integers. The integers that you see in MATLAB are much closer to C's "file descriptors", and if you mix up the two then you will get an compiling error or you will get a crash.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!