2026-03-30 6:15 AM - last edited on 2026-03-31 2:32 AM by Gyessine
Title edited to clarify that this is about accessing a file located on a Host PC
hello ST Community,
I'm trying to open a binary file with the fopen function for a STM32L5 microcontroller, but it always gives me a null value. The code is like this:
char filepath[256];
sprintf(filepath,"%s","C:\\fileBIN.bin");
FILE *file;
file = fopen(filepath,"rb");
if(file == NULL)
{
printf("Error occurred opening file. \r\n");
}
else
{
printf("File opened successfully. \r\n");
}
It always gives me a null value but the path exists and is correct. What's wrong?
Thanks for supports
Solved! Go to Solution.
2026-03-30 8:37 AM - edited 2026-03-30 8:46 AM
You never said that's what you're trying to achieve in this thread!
In that case, you will need to have something on the PC which sends the data to the Target.
The PC pushes to the Target - the Target doesn't pull from the PC.
CubeProgrammer can do this, and is ideal for use with the STM32 System Bootloader:
AN2606: Introduction to system memory boot mode on STM32 MCUs
AN3155: How to use USART protocol in bootloader on STM32 MCUs
If you're making a custom bootloader, then you'll have to define your own protocol; eg,
AN4657: STM32 in-application programming (IAP) using the USART - uses the YMODEM protocol, which is widely supported by terminal emulators & serial comms libraries/frameworks.
There's also ST Open Bootloader - also works with CubeProgrammer
2026-03-30 6:43 AM - edited 2026-03-30 6:44 AM
Hi,
no info: what you use + did...
use ? FATFS ?
what drive ? C ??
interface ? SD-card ?
did you mount the volume ?
2026-03-30 6:55 AM
@AScha.3 wrote:no info: what you use + did...
Indeed!
@AScha.3 wrote:use ? FATFS ?
I don't think it's FatFs: that has f_open rather than fopen, and (AFAIK) uses drive numbers - not letters ?
@Domy_ST please see: How to write your question to maximize your chances to find a solution
2026-03-30 7:08 AM
@Domy_ST wrote:the fopen function ... always gives me a null value.
In that case, errno (or something similar) should be set to an error code showing what went wrong...
As previously noted, you need to give a lot more details about your setup.
2026-03-30 7:12 AM
Hello @Domy_ST
If you want to read file system from SD card, you need ti use FATFS library.
Please refer to the examples below:
2026-03-30 7:21 AM - edited 2026-03-30 7:28 AM
@Andrew Neil wrote:I don't think it's FatFs:
Also doesn't look like FileX: that appears to have fx_file_open
@Domy_ST - You're not trying to open a file that's on your Host PC, are you?
For that, you'd need Semihosting
How to use semihosting with STM32CubeIDE and STM32
2026-03-30 7:24 AM
@Saket_Om wrote:you need to use FATFS library.
* other embedded file system libraries are available
2026-03-30 7:27 AM
no the file is exactly on the local PC the fopen function does not find it
2026-03-30 7:42 AM - edited 2026-03-30 7:48 AM
If you had two PCs sitting side-by-side on your desk, one PC would not be able to see files on the other - unless you add networking of some sort.
Similarly, your microcontroller can't see any resources on your PC unless you provide suitable connectivity.
As mentioned earlier, Semihosting may be an option for you?
I think you need to take a step back, and explain what you're actually trying to achieve here - this sounds very much like an X-Y problem.
Describe the goal, not the step
PS:
Is there a clue in the filename: "C:\\fileBIN.bin" ?
Are you, perhaps, trying to do a firmware update?
As noted in your previous thread, you'd use a bootloader for that - not try to access the file on the Host PC
2026-03-30 7:46 AM - edited 2026-03-30 7:50 AM
The goal I'm trying to achieve is simply to read the file contents in binary and assign 32 bits of hex to one array parameter (uint32_t) at a time and overwrite it to flash memory (sort of bootloader). I get the file path by receiving via UART.