Problem reading .txt files with the SD library

I have simplified my code problem to the following one:

#include <SD.h>

File readFile;
const int pino_chip_select = 53;

void setup() {
  Serial.begin(9600);
  SD.begin(pino_chip_select);

  printSD();
}

void loop() {

}

void  printSD() {
  readFile = SD.open("file.txt");
  while (readFile.available()) {
    char aux_char = readFile.read();
    Serial.println(aux_char);
  }
}

Basically what this code does is to print all the information inside the “file.txt” that is written on my sd card. My problem is that to some txt files that I generate on my computer this program doesn’t work. It seems that it doesn’t recognize any of the information in the file that I have created…

Am I missing something in this code? Is it supposed to work fine?
I’ve tried to change the SD partition to FAT16 but it didn’t work as well. Does anyone know why I can read some .txt files with this code and others I can’t?

Why don't you attach a file that CAN be read and another that CAN'T be read?
Without seeing what works and what doesn't, there isn't much we could say specifically.

Test what SD.open returns...

What is linha for? Remains from previous code?

Yeah, the String linha was from my previous code. I have removed it from my post now…

I have changed my printSD function to this:

void  printSD() {
  readFile = SD.open("file2.txt");
  Serial.println("Beginning to print: ");
  while (readFile.available()) {
    char aux_char = readFile.read();
    Serial.println(aux_char);
  }
  Serial.print("readFile return: ");
  Serial.println(readFile);
}

Basically I made the following tests with two files, file1.txt and file2.txt (by changing the name of the file in the line readFile = SD.open(“file2.txt”). They both have exactly the same content.

file1.txt:

G28
M106

Serial Monitor Output of file1.txt:

Beginning to print: 
X
1
0
8
.
8
9
9
9
readFile return: 1

file2.txt:

G28
M106

Serial Monitor Output of file2.txt:

Beginning to print: 
G
2
8


M
1
0
6


readFile return: 1

Different outputs to the same content isn’t making any sense to me at all. It seems that when I create new text inside the .txt file on my computer the Arduino doesn’t recognize what I wrote.

I have tried to use the notepad++ on windows and the gedit on Ubuntu…
Also, I’ve traded my SD module connected to the Arduino Mega board. But the output is the same as above for both of my SD modules, so I believe it isn’t a problem with it.

PS: If I trade the names of the files file1.txt and file2.txt to file2.txt and file1.txt the one that will print the content correct will be the file1.txt…

Give us the 2 files as they are on the SD card

and try with this

void  printSD() {
  if (readFile = SD.open("file2.txt", FILE_READ)) {
    Serial.println(F("Beginning to print:\n--------------"));
    while (readFile.available()) {
      char aux_char = readFile.read();
      Serial.print(aux_char);
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("File access error: ");
  }
}

With your new printSD method the outputs are like this:

file1.txt

Beginning to print: 
X108.8999
--------------

file2.txt

Beginning to print: 
G28
M106

--------------

I have attached the file1.txt and file2.txt that I’m using here as well…

file1.txt (9 Bytes)

file1.txt (9 Bytes)

Ok - try with this code - ensure both files are on the SD.

void  printSD() {
  int data;

  if (readFile = SD.open("file1.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file1.txt:\n--------------"));
    while ((data = readFile.read()) != -1) {
      Serial.print((char) data);
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file1.txt access error: ");
  }

  if (readFile = SD.open("file2.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file2.tx:\n--------------"));
    while ((data = readFile.read()) != -1) {
      Serial.print((char) data);
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file2.txt access error: ");
  }
}

Both files are recognized but now they don't show any content in any of them:

Serial Monitor:

Beginning to print file1.txt:
--------------

--------------
Beginning to print file2.tx:
--------------

--------------

post the full code you used

#include <SD.h>

File readFile;
const int pino_chip_select = 53;

void setup() {
  Serial.begin(9600);
  SD.begin(pino_chip_select);

  printSD();
}

void loop() {

}

void  printSD() {
  int data;
  if (readFile = SD.open("file1.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file1.txt:\n--------------"));
    while (data = readFile.read() != -1) {
      Serial.print((char) data);
    }
   Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file1.txt access error: ");
  }

  if (readFile = SD.open("file2.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file2.tx:\n--------------"));
    while (data = readFile.read() != -1) {
      Serial.print((char) data);
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file2.txt access error: ");
  }
}//*/

OK try this

#include <SD.h>
File readFile;
const int pino_chip_select = SS;

void  printSD() {

  if (readFile = SD.open("file1.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file1.txt:\n--------------"));
    while (readFile.available()) {
      Serial.print((char) readFile.read());
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file1.txt access error: ");
  }

  if (readFile = SD.open("file2.txt", FILE_READ)) {
    Serial.println(F("Beginning to print file2.tx:\n--------------"));
    while (readFile.available()) {
      Serial.print((char) readFile.read());
    }
    Serial.println(F("\n--------------"));
    readFile.close();
  } else {
    Serial.println("file2.txt access error: ");
  }
}

void setup() {
  Serial.begin(9600);
  SD.begin(pino_chip_select);
  printSD();
}

void loop() {}

I tried it with my set up and your exact same 2 files and all worked fine.

if that does not work, I suggest you check what SD.begin(pino_chip_select) returns, may be you are not wired the right way.

I also checked with the SDFat library which I prefer to use and all worked fine too

of course using the latest IDE and most up to date libraries