4 years, 1 month ago.

How to read a file in file system

Hi, Here is the thing: I'm doing a project which I wanna the EFM32 Giant Gecko board be able to read the .txt file from its filesystem. So is there anyone who knows how am I suppose to do it?

Right now, I am using the stand way that how to read a file in a pc.

I have a function like this:

  1. include <stdio.h>
  2. include <stdlib.h>

char *readFile(char *fileName) { FILE *file = fopen(fileName, "r"); char *code; size_t n = 0; int c;

if (file == NULL) return NULL; could not open file fseek(file, 0, SEEK_END); long f_size = ftell(file); fseek(file, 0, SEEK_SET); code = (char *)malloc(f_size);

while ((c = fgetc(file)) != EOF) { code[n++] = (char)c; }

code[n] = '\0';

return code; }

And in my main I want to call it, and assign the return value to an Array

int main() { char *script; script = readFile("F:\s.txt"); luaL_loadstring(l, script); lua_pcall(l, 0, 0, 0);

return 0; }

But it seems the function can never reach the file which I specify. When I debug the program, it always stops at "if (file == NULL) return NULL; could not open file" So I suppose the board just can't find the file,is there anyone who knows how to fix this xD

Question relating to:

Silicon Labs' EFM32™ Giant Gecko ARM® Cortex®-M3 based 32-bit microcontrollers (MCUs) provide flash memory configurations up to 1024 kB, 64-128 kB of RAM and CPU speeds up to 48 MHz, ...

One should use the LocalFileSystem (https://developer.mbed.org/handbook/LocalFileSystem ) to read the files that are accessible via the USB disk. I'm don't know whether this works for the GG, but its worth a try.

posted by Hendrik Lipka 23 Feb 2016

1 Answer

4 years, 1 month ago.

Hi,

Basically, there is no way for the EFM32 to access the file system you see on the computer and use to program your device. This file system, besides being a virtual file system with no real storage, only exists in the board controller (debugger) domain.

Perhaps you can read in the data from the serial port instead?

Accepted Answer

Hi, again I tried to use the serial port, but there is a problem, when I am in the debug mode, the data can't be read in my program, since both the debug and serial use the same usb port. So I tried another way, which is I put the .txt file in the file system of my computer. I am using IAR Embedded Workbench IDE, and I m running it on a virtual machine cause the license problem. But still the code does't work, I have set the library in the full mode to support the <stdio.h>. Then I tried to wirte a .txt file into my file system of my virtual machine instead. Basically the code looks like:

int main(){ char *fileName="C:
s1.txt"; FILE *file = fopen(fileName, "w");

char *code; size_t n = 0; int c;

if (file == NULL) return NULL; code = (char *)malloc(1000); while ((c = fgetc(file)) != EOF) { code[n++] = (char)c; } code[n] = '\0'; }

But still, I can't make it working. Do you have any idea how to solve this?

posted by Wenjie Jiang 30 Oct 2015