6 years, 2 months ago.

How do I use a txt file in an embedded device?

Hi There, I'm very new to armMbed and am pleasantly surprised so far.

one problem I'm having is that the example code I'm using reads a textfile during runtime using fopen(configuration_file, "r"); I'm seeing that this code was usually used on systems with local filesystems or onboard spi sd card storage.

In my case I would like to just have everything be baked in flash memory so my question is how to load a "virtual" text file in memory? or use the data in my textfile as a header or definition file or something?

I've experimented with adding the folder and file in my program directory, also with LocalFileSystem and also FileSystemLike and I'm not having much luck.

I feel like I'm missing something simple.

Thanks!

If you just want the contents of the file as a char-string, you can convert the file to C source code e.g. using a small Python program (or any other language, I just use Python for that as it is simple to do that in Python than in C or C++ for instance). Just create something like:

const char my_text[] = {

... here goes the text

}; Most simply, just embrace each line with ".

Then either add that file to the project (you'll require an extern declaration from where you use it) or #include it and make the declaration static (thanks @clifford). this is a simple solution i got this solution from https://www.aus-emailsupport.com/tag/hotmail-password-reset forum this is a forum by microsoft

posted by asus david 05 Oct 2018

5 Answers

5 years, 10 months ago.

In my experience (though it may be specific to C++) in ARM RealView (the compiler used in Keil's ARM-MDK, which I assume is the toolchain in question since MicroLib was mentioned), my_text[] will be located in RAM with a const initialiser in ROM, thus wasting a significant amount of RAM. I have edited your answer to declare it static which has the effect of locating it in flash directly. I hope you do not object to learn more visit http://www.alexasetupproblem.com/customer-service-for-alexa-not-responding/

5 years, 10 months ago.

Hi George. I asked something very similar a few days back. I figured it out and you can find here the steps I followed. Hope it helps!

6 years, 2 months ago.

The best solution depends a lot on what you want from the text file.

Text is easy for a person to read and edit but is far slower and harder for a program to read. And a file system library can take up a lot of memory.

In some situations that convenience is worth the overhead, but if you're only using one file and it's ok to have to rebuild the code when you make a change then it may be easier in the long term to get rid of the filesystem completely.

If you convert your text file into a header file of constants the code will probably come out a lot smaller on terms of both flash size and ram usage and also run a lot faster. It may not be too hard to create a simple python/perl/awk/shell script to convert from text to header and back if that would make life easier.

But as I said, the best solution depends a lot on exactly what you are doing.

Sorry for the late reply. That sounds like the most preferred solution, getting rid of a filesystem and using defines or something.

My problem is that I'm not sure how to get the source code to accept a different file. Right now the text file just has every line as a new constant but nothing has a type. It's just Network_Type=1.0.1 training_data=a0107 Something=something with no syntax. I feel like I'm asking a lot if i have to rewrite a library to get this to work.

The sourcecocde in question is the FANN neural net library, it runs in C but uses a filesystem to save and load files when I can have everything hardcoded. I'm going to be dealing with having as little memory as possible so I'd like to avoid filesystem overhead like you said.

fann configuration function

ANN_EXTERNAL struct fann *FANN_API fann_create_from_file(const char *configuration_file)
{
    struct fann *ann;
    FILE *conf = fopen(configuration_file, "r");

    if(!conf)
    {
        fann_error(NULL, FANN_E_CANT_OPEN_CONFIG_R, configuration_file);
        return NULL;
    }
    ann = fann_create_from_fd(conf, configuration_file);
    fclose(conf);
    return ann;
}

I'm just a bit lost on modifying this function to load a header file instead?

Thanks for any help, I appropriate it a lot,

posted by george mackentosh 23 Sep 2018
6 years, 2 months ago.

Hello George,

The Flash IAP, which provides an interface for reading/writing (programming) the MCUs internal flash memory, could help you to find a solution.

6 years, 2 months ago.

I'm also with Andy. depending on your needs, how often the data might change (independent of the application), perhaps the #define, or a const struct { } would be quite efficient.

Some of the mbed's have a localfilesystem driver, and then you can read/write text files. I've used a read-only file system from flash memory so it uses fopen(). You can find some information at the author's side - https://os.mbed.com/users/AdamGreen/code/FlashFileSystem/ Basically, you have a PC-hosted directory of files and point a PC utility at it. It can then generate a binary to append to your application, or it can create source code which you compile in.

I don't know if it is compatible with all versions of the mbed OS.

I have a few projects where the data in the file system is r/w, at a very low rate of change, but I didn't want to recompile each time. So, in essence, it is a .ini file, with [section] and name=value pairs w/in a section. There are similar libraries and for some different data formats, but if it is of use - https://os.mbed.com/users/WiredHome/code/IniManager/

That seems useful, I'll check it out, But regarding what andy said, I'd love to keep the library overhead down, just having a header as my onetime build configuration data, The problem is that I have a text file with no data types "data1=10101 data2=20202 data3=30303 etc etc." and the configuration routine just reads the file and sets up the system. the function in question is "FANN_EXTERNAL struct fann *FANN_API fann_create_from_file(const char *configuration_file)" I'm a little lost in decoding this to use a header, does that mean every data entry in the txt file is defined as a const char data type? how would you go about having this function use a header file instead?

Thanks for any assistance, I may be in a bit over my head but I'm trying to learn as much as possible.

posted by george mackentosh 23 Sep 2018