1

I am working with an NXP microcontroller, which has stdio functionality when connected to the debugger. I've had no issues using fwrite() to write data to a binary file, so that I can monitor and plot values with an external program. I'm now trying to go the other way, but I can't get fread() to work. I've reduced the code to the simplest example I can think of, and it still doesn't read the file:

#include <stdio.h>
int main (void)
{
 FILE *fp;
 int16_t writeData[5] = {1, 2, 3, 4, 5};
 int16_t readData[5] = {0, 0, 0, 0, 0};
 int16_t nData;
 // write to file
 fp = fopen("testFile.dat", "wb");
 nData = fwrite(&writeData, sizeof(uint16_t), 5, fp);
 fclose(fp);
 // read from file
 fp = fopen("testFile.dat", "rb");
 nData = fread(&readData, sizeof(uint16_t), 5, fp);
 fclose(fp);
}

I'm stepping through the code line-by-line. after writing the file, nData = 5 and the file contents are as expected "01 00 02 00 03 00 04 00 05 00" (observed using Windows PowerShell command format-hex "testFile.dat"). After reading the file, nData = 0 and readData is not changed.

I've checked if (fp = NULL) after calling fopen(), that's not the issue. I've tried calling ferror(fp) after each line in the read section, it always returns 0. I've also had no luck with the agruments readData, &readData[0], and readData[0] (doesn't make sense/seem necessary respectively, but I've seen these variations in some examples). The "required supporting OS subroutines" are the same for both fread() and fwrite(), so that's not the issue either.

Any suggestions?

asked Nov 6, 2024 at 20:12
7
  • 1
    Why the mismatch between the type used in the array and the sizeof? Wouldn't expect it to make a difference, but could lead to UB... Commented Nov 6, 2024 at 20:25
  • 3
    You don't need & before readData or writeData. Arrays automatically decay to pointers when used as function arguments. Commented Nov 6, 2024 at 20:29
  • 1
    It sounds like you are using semihosting functionality. It is possible that only the output part is implemented by the library. Commented Nov 6, 2024 at 20:31
  • @EugeneSh. You are correct, I am using semihosting. This was my fear as well, perhaps it's not supported. As far as I can tell fread is implemented, it's at least declared in stdio.h and has doxygen documentation when I mouse over it in my IDE (MCUXpresso). I'll take a look for any programs that use it in the NXP-provided example code for my development board to be sure. Commented Nov 6, 2024 at 22:53
  • You could try reading from stdin for starters Commented Nov 6, 2024 at 23:07

1 Answer 1

1
  • To be sure the correct byte size is used:

try:

(sizeof(int16_t)

instead of

sizeof(uint16_t))
  • return values should be checked.
  • Error checking is also a good practice.
answered Nov 6, 2024 at 20:40
Sign up to request clarification or add additional context in comments.

2 Comments

Good eye, I didn't notice this. Unfortunately changing the type in sizeof (or changing the types of the arrays for that matter) doesn't fix the issue.
Well, since int16_t and uint16_t are both 16-bits integer types, it won't make any difference on the outcome.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.