[Solved] Count Lines and Create Array

Objective: Read values from a txt file on an SD card and generate an array. Each value is stored on a separate line.

Text file example:


I want to end up with an array like this:

long sidArray[] = {12345678, 12345, 1445567};

I using this thread as a reference.

My code so far :

#include <SdFat.h>
#include <SPI.h>

int lineCount = 0;
int inputChar;
char myArray[100];

SdFat Sd;
File datFile;

void setup() {
    pinMode(4, OUTPUT);
    pinMode(10, OUTPUT);
//  pinMode(53, OUTPUT);
    digitalWrite(4, LOW); // Enable SDCard
    digitalWrite(10, HIGH);  // Disable Ethernet
//  digitalWrite(53, HIGH);  // Disable Ethernet (Mega)

//  Start Serial
    Serial.println("Serial Started");

    if(!Sd.begin(4, SPI_HALF_SPEED)) Sd.initErrorHalt();

    datFile = Sd.open("sidList.txt", "r");
    if(!datFile) Serial.println("Error - datFile");

    while (datFile.available())
/*      while((inputChar=fgetc(datFile))!=EOF) {
            myArray[linecount] = "...contents of line being read...";
            if(inputChar == '\n') linecount++;
        while(inputChar != EOF) {
            inputChar = datFile.read();
            myArray[linecount] = "...contents of line being read...";
            if(inputChar == '\n') linecount++;

void loop() {
  // put your main code here, to run repeatedly:


I think I am close, but I am having trouble figuring out how to store the contents of each line into myArray.
Once I have char myArray I will convert it to long sidArray, unless it can be done directly…?
FWIW code is untested.

Help appreciated.

myArray[linecount] = "...contents of line being read...";

It’s going to be hard squeezing that long string into a single char.

Look up strtol().

Look up strtol().

The conversion was not my stumbling block, the extraction was.
Anyway I was able to figure out a solution.