Parsing TXT file values from SD into Array

Hello,
I have some values in TXT file on SD card.
All I'm trying to do is to get first 10 values from file and store them into array.

I have managed to read whole file (74 values example) and build array correctly, but if try to count up to 10 lines only with this code:

if (arrayC++ > 10){break;}

it somehow does only even steps and fills array at even positions.

Any ideas?

Code:

#include <SPI.h>
#include <SD.h>
File myFile;

int some_array[10];
int arrayC = 0;

void setup() { 
  Serial.begin(9600);       
  readSD();
}

void loop () {}


void readSD() {
  if (!SD.begin(53)) {
    Serial.print("NO SD CARD!");
    return;
  }
  Serial.println("SD CARD FOUND");
  myFile = SD.open("2.txt");
  
  if (myFile) {
    Serial.println("FILE FOUND");  

  while (myFile.available()) {
    switch ((char)myFile.peek()) {
      case ',': 
      myFile.read();
      break;
      case '\r':
      myFile.read();
      break;
       
      default:
      int newV = myFile.parseInt();   
      some_array[arrayC] = newV;
      arrayC++;
      //if (arrayC++ > 10){arrayC = 10; break;}
      Serial.println(arrayC);       
      break;
      }
    }
    
  myFile.close();
  Serial.println("PARSE DONE"); 
  }
   
  for (int i = 0; i < 10; i++){
    Serial.print("I=");    Serial.print(i);    Serial.print(" = ");
    Serial.println(some_array[i]);
  }
  
  Serial.println("ALL DONE!"); 
}

2.txt File:

125,
685,
247,
687,
957,
325,
698,
384,
397,
568,
897,
125,
685,
124,
687,
957,
325,
698,
568,
etc....

Serial output with IF line:

SD CARD FOUND
FILE FOUND
2
4
6
8
10
PARSE DONE
I=0 = 125
I=1 = 0
I=2 = 685
I=3 = 0
I=4 = 247
I=5 = 0
I=6 = 687
I=7 = 0
I=8 = 957
I=9 = 0
ALL DONE!

Serial output without IF line:

SD CARD FOUND.           
FILE FOUND
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
PARSE DONE
I=0 = 125
I=1 = 685
I=2 = 247
I=3 = 687
I=4 = 957
I=5 = 325
I=6 = 698
I=7 = 384
I=8 = 397
I=9 = 568
ALL DONE!

The ++ in your if statement is incrementing arrayC, get rid of it.

      arrayC++;
      //if (arrayC++ > 10){arrayC = 10; break;}

I am not sure what you have tried but if you were simply to uncomment the line in your code then you will have incremented arrayC twice. Is that what you did ?

Whilst it seems neat to increment a variable as part of a test it does mean that it can be more difficult to debug. It may be better to increment the variable before the test so that you can print it to see what is going on

Thank you so much, I never noticed I had ++ there :frowning: