Can't read txt file on nucleo f303k8

Hello! I am having an issue with reading a txt file on my Nucleo-f303k8. Here is my code:
#include <stdio.h>

#include <stdlib.h>

#include “mbed.h”

#include <errno.h>

void file_read() { // array heeft lengte van array_size, elke array bevat 3 coordinaten

FILE *filepointer = fopen("C:\\test.txt", "r"); 

                                 

if (filepointer == NULL) {

  printf("Can't open file\n");

} else {

   int one_or_zero = 0;

   int x = 0;

   int y = 0;

   while (!feof(filepointer)) {

       fscanf(filepointer, "%d%d%d\n", &one_or_zero, &x, &y);

       printf("%d %d %d\n", one_or_zero, x, y);

   }

}

fclose(filepointer);

}

int main(){

file_read();

}

and this is what the txt file says:

1 100 20
0 30 40
1 20 60

When i compile this there are no problems. But when i load the code on to my NucleoF303K8, Realterm gives me the following error:

++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0 : 080003AF
R1 : 016C8000
R2 : 08004456
R3 : 00000019
R4 : 00000000
R5 : 080002DF
R6 : 080002E5
R7 : FFFFFFFF
R8 : 080002DF
R9 : 00000000
R10 : 08006AF0
R11 : 08006AF0
R12 : 01010101
SP : 20002FB8
LR : 08000AD7
PC : 080013AA
xPSR : 01000000
PSP : 00000000
MSP : 20002F50
CPUID: 410FC241
HFSR : 40000000
MMFSR: 00000000
BFSR : 00000004
UFSR : 00000000
DFSR : 00000008
AFSR : 00000000
Mode : Thread
Priv : Privileged
Stack: MSP

– MbedOS Fault Handler –

++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x800519F
Error Value: 0x80013AA
For more info, visit: mbedos-error
– MbedOS Error Info –

I visited the links provided in the error message but they made me none the wiser. Can anyone see what’s wrong with my code?

Hi there,

please use correct code formatting like in the following example

```
 //your code
```

You try to open a file from not exist place. Because of that your pointer to the FILE is still NULL. That mean it was never opened and you call method fclose() anyway.

void file_read() { // array heeft lengte van array_size, elke array bevat 3 coordinaten
    FILE *filepointer = fopen("C:\\test.txt", "r");             
    if (filepointer == NULL) {
        printf("Can't open file\n");
    } else {
       int one_or_zero = 0;
       int x = 0;
       int y = 0;
       while (!feof(filepointer)) {
           fscanf(filepointer, "%d%d%d\n", &one_or_zero, &x, &y);
           printf("%d %d %d\n", one_or_zero, x, y);
       }
       fclose(filepointer); /*correct place*/
    }
    //fclose(filepointer); /*wrong place and that is the reason of crash*/
}

BR, Jan

Hi Jan,

What do you mean by “you try to open a file from a non existent place”? I’m trying to open it from my C disk on my laptop.

I think you can’t open any file form your PC directly. The device can listen to the serial, where an application on the PC side is reading the file and sending the data.

that was previously possible with mbed2 and LocalFileSystem, but that was supported only on the Grandpa LPC1768 and LPC11U24.