Add to code to compile a dynamic content from a text file

I searched online without success. Found 100 of results, but not the solution.

I want to read the content of 35 external text files in my code and have them compiled when the code is being compiled.

The content of one text file is an array:
const int16_t EXAMPLE[] = {a huge amount number};

For now, the content of these 35 text files is paste in the code by hand, then compiled. But this is a painful solution, cause I’d like to be able to choose 35 files out of 2000 and try different combinations so to say.
Having them externally would allow me (and others) to simply drop 35 files which were chosen into a directory, rename them from 0 to 34 and read them in the code, compile. Done.

Of course it would be smarter to have a SD Card with all files insides and read them while the code is executing, but it can’t be done here in this project, because there’s no way a SD card can be added to the hardware which already exists and all pins are also taken anyway. I only want to simplify the process of rying different combinations.

So I need to be able in my .ino file to add a script picking the content on each text file, one by one (like going through an array).

I have a directory myDirectory with my .h and .ino files inside.
I’d then add my 35 files (like „0.txt“, „1.txt“ etc) into myDirectory or another directory inside myDirectory and read them into the code.

So instead of having all my huge arrays of numbers paste by hand in my .ino file, I’d have in my .ino file something like (oversimplified):
myArray1 = the array in my text file 0.txt
myArray2 = the array in my text file 1.txt

which I’d try later to put in a loop instead, so no need to write 35 lines, but only one. Something I’d do in php like this and that I’ll will learn to do in C.

for ($n = 0; $n <= 34; $n++) {
„myArray“+n = n+“.txt“
}

To simplify for now, it means I first need to know if it’s possible to say in the code to go and look for a file named „0.txt“ in a directory called „whatever“ and read it’s content, without the use of a SD file. No runtime here. I only want add dynamic content to my code to be compiled.

More info:
I searched online but couldn’t find an answer. On this forum I search for +read text file“, there are a lot of results, but I don’t see the solution:
Read set up text file at compile - Programming Questions - Arduino Forum
How to import a txt file to arduino ? - Programming Questions - Arduino Forum
Reading data from a Text file - Programming Questions - Arduino Forum
Reading Text Files - Syntax & Programs - Arduino Forum
They seem to refer to the arduino chip reading the file via serial.
I did read this
SD - Arduino Reference
But what I am asking is how I can read the content of a text file BEFORE compiling.
It’s not about reading a file from a SD Card while the compiled code is executing on an Arduino hardware. And as mentionned, no pin on the hardware is free anymore

Sounds like something that would be fairly easy using preprocessor instructions. Just keep all 2000 files in the same directory and #include only the 35 you need.

Please provide a concrete (but scaled down) example of what you're trying to do. Include code and samples of what these "35 files" look like. Be concise but complete.

gfvalvo:
Please provide a concrete (but scaled down) example of what you're trying to do. Include code and samples of what these "35 files" look like. Be concise but complete.

Text file "0.txt" content is (I simplified cause each array has hundred of elements, but the principle remains the same):
const int16_t EX0[] = {1,2,3,4};

Text file "1.txt" content is:
const int16_t EX1[] = {5,6,7,8};

Text file "2.txt" content is:
const int16_t EX2[] = {9,10,11,12};

For now,
const int16_t EX0[] = {1,2,3,4};
const int16_t EX1[] = {5,6,7,8};
const int16_t EX2[] = {9,10,11,12};
is paste directly into my code. An .ino file. Then compiled.

I want to be able to read as explained in the original post.

But what aarg wrote about the #include might be the part I was missing. I'm gonna try this, replacing .txt by .h.

newbie499:
But what aarg wrote about the #include might be the part I was missing. I'm gonna try this, replacing .txt by .h.

Variable definitions don't belong in .h files. Those should go in a separate .cpp file with the 'extern' declarations in the .h file.

Does each file contain the same number of arrays?

Please supply a short complete code of what you're trying to do. It should compile. Use CODE TAGS.

gfvalvo:
Variable definitions don't belong in .h files. Those should go in a separate .cpp file with the 'extern' declarations in the .h file.

Yeah I treid with the .h file and got an error.

gfvalvo:
Does each file contain the same number of arrays?

Each text file contains one array.
Each array length can be different (around 540 objects). But it does not matter.
Each array has a different name, but also this I'll handle later in the main .ino code once I know how to get each array dynamically.

gfvalvo:
Please supply a short complete code of what you're trying to do. It should compile. Use CODE TAGS.

17 different .ino files with 6700 lines of code. I'll see what I can do better.
Before I am going to read more about #include.

This is the content of "1.txt" for instance:

const int16_t EX1[] = {
    293,1911,3700,5551,7554,9597,11701,13729,15623,17169,18132,20374,22668,24395,26214,27780,
    29288,30458,31453,32104,32568,32738,32740,32499,32112,31540,30857,30043,29147,28139,27060,25936,
    24744,23470,22100,20621,19009,17270,15386,13379,11238,8997,6655,4267,1837,-554,-2912,-5139,
    -7262,-9133,-10822,-11867,-13197,-15051,-16448,-17712,-18855,-19914,-20813,-21570,-22157,-22598,-22895,-23057,
    -23087,-22987,-22767,-22425,-21982,-21435,-20806,-20091,-19311,-18461,-17557,-16597,-15594,-14547,-13470,-12360,
    -11232,-10087,-8932,-7773,-6617,-5469,-4332,-3215,-2117,-1050,-11,989,1954,2873,3753,4576,
    5357,6076,6747,7354,7907,8388,8808,9134,9346,9721,10053,10301,10542,10749,10944,11097,
    11233,11331,11412,11461,11492,11495,11482,11445,11389,11314,11226,11120,11002,10872,10731,10577,
    10415,10242,10059,9865,9661,9449,9224,8991,8745,8489,8221,7940,7645,7338,7015,6679,
    6329,5962,5581,5184,4771,4344,3902,3446,2978,2495,2003,1500,988,467,-60,-591,
    -1127,-1664,-2204,-2742,-3280,-3814,-4342,-4865,-5382,-5890,-6391,-6881,-7359,-7827,-8284,-8728,
    -9159,-9575,-9977,-10365,-10738,-11096,-11436,-11759,-12064,-12349,-12616,-12860,-13081,-13280,-13454,-13602,
    -13722,-13813,-13876,-13906,-13905,-13873,-13807,-13708,-13576,-13411,-13213,-12985,-12726,-12437,-12121,-11779,
    -11411,-11025,-10616,-10194,-9754,-9307,-8849,-8391,-7927,-7471,-7014,-6573,-6136,-5720,-5314,-4931,
    -4563,-4214,-3883,-3564,-3258,-2946,-2630,-2233,-2013,-1849,-1652,-1496,-1356,-1238,-1122,-1006,
    -878,-730,-562,-353,-110,186,536,955,1435,1994,2631,3350,4167,5073,6164,7497,
    8801,9818,10825,11490,12719,14337,15723,17280,18812,20402,21895,23326,24599,25736,26680,27445,
    28001,28361,28520,28495,28301,27955,27486,26905,26246,25509,24727,23889,23026,22113,21172,20178,
    19142,18035,16865,15597,14244,12775,11201,9504,7701,5784,3780,1700,-428,-2576,-4718,-6812,
    -8836,-10731,-12490,-14028,-15324,-16089,-17311,-18848,-19929,-20963,-21883,-22741,-23457,-24054,-24506,-24839,
    -25048,-25146,-25131,-25007,-24779,-24453,-24038,-23540,-22976,-22340,-21651,-20906,-20118,-19283,-18415,-17509,
    -16579,-15620,-14644,-13649,-12644,-11627,-10608,-9584,-8563,-7544,-6530,-5525,-4533,-3552,-2586,-1639,
    -710,197,1083,1944,2778,3587,4367,5117,5839,6529,7188,7815,8411,8975,9507,10007,
    10477,10916,11325,11705,12057,12382,12681,12953,13202,13426,13628,13808,13967,14105,14222,14320,
    14398,14456,14493,14511,14509,14486,14441,14374,14285,14173,14036,13876,13690,13479,13241,12978,
    12689,12375,12035,11670,11282,10870,10436,9980,9507,9014,8504,7980,7444,6894,6332,5763,
    5184,4600,4009,3414,2817,2216,1614,1011,410,-189,-787,-1382,-1974,-2560,-3142,-3717,
    -4288,-4852,-5407,-5956,-6496,-7028,-7549,-8062,-8562,-9051,-9526,-9987,-10432,-10861,-11272,-11662,
    -12030,-12374,-12694,-12985,-13250,-13483,-13685,-13855,-13990,-14090,-14158,-14188,-14186,-14149,-14080,-13978,
    -13846,-13687,-13503,-13297,-13071,-12831,-12578,-12317,-12054,-11791,-11534,-11289,-11060,-10856,-10682,-10554,
    -10530,-10739,-10886,-10899,-10935,-11004,-11118,-11266,-11438,-11619,-11799,-11962,-12102,-12201,-12262,-12262,
    -12209,-12077,-11890,-11611,-11275,-10840,-10349,-9753,-9105,-8342,-7525,-6580,-5569,-4398,-3098,-1439
};

It means for now I have exactly this paste in my .ino file. With other 34 different arrays.

Thanx again for mentioning #include and .cpp / extern declaration.
I will now google for passing .cpp vars to .ino.

Yeah I treid with the .h file and got an error.

What is the error message.
The .h file either needs to be in the same directory as the .ino file, or you need to specify the path. Use quotes in the #include statement, not < and >.

Using the .h file for a variable definition isn't proper convention, but will work.

gfvalvo:
Variable definitions don't belong in .h files.

Perhaps. But they will work fine in a .h file. I do it regularly.

...R

Robin2:
Perhaps. But they will work fine in a .h file. .......

They work until you include that .h file into multiple .cpp files. Then the linker will be quite unhappy with you.

Create the following directory structure

C:\USERS\yourUserName\DOCUMENTS\ARDUINO\FORUM\yourSketch
|
+--- yourSketch.ino
|
\--- src
    |
    +---EX
    |
    +---- EX0.h
    +----  EX1.h

Content of the EX0.h

#ifndef _EX0_H_
#define _EX0_H_

char EX0[] = "Hello";

#endif

Content of the EX1.h

#ifndef _EX1_H_
#define _EX1_H_

char EX1[] = "World";

#endif

Sketch example

#include "src\EX\EX0.h"
#include "src\EX\EX1.h"

void setup()
{
  Serial.begin(57600);
}

void loop()
{
  Serial.println(EX0);
  Serial.println(EX1);
}

If you feel like using .cpp as well, this will be EX0.h

#ifndef _EX0_H_
#define _EX0_H_

extern char EX0[];

#endif

and this will be EX0.cpp

char EX0[] = "Hello";

All .cpp and .h files in src\EX

gfvalvo:
They work until you include that .h file into multiple .cpp files. Then the linker will be quite unhappy with you.

I'm much too stupid to do that. I never use .cpp files. :slight_smile:

...R

sterretje:
Sketch example

#include "src\EX\EX0.h"

#include "src\EX\EX1.h"

void setup()
{
  Serial.begin(57600);
}

void loop()
{
  Serial.println(EX0);
  Serial.println(EX1);
}

If you had a myCode.ino file where you have your setup() function and a myLoop.ino with loop() function, what would be the best method to pass the variables to myLoop.ino, after declaring in the myCode.ino:

#include "src\EX\EX0.h"
#include "src\EX\EX1.h"