there's a txt file in my sd card and I want to show the content on my LCD(1602)
the txt is written by line, so how can I read the txt by line?
something like this; (not tested)
char buffer[20];
int idx = 0;
readLine()
{
bool EOL = false;
while (! EOL)
{
c = readCharSD(); / reads 1 char from SD
if (c == '\n' || idx==19) // prevent buffer overflow too..
{
buffer[idx] = 0;
idx = 0;
EOL = true;
}
else
{
buffer[idx] = c;
idx++
}
}
}
The SdFat library has a function to get text lines. It works like the Linux fgets function http://www.cplusplus.com/reference/clibrary/cstdio/fgets/.
Here is an example function that reads lines from a file:
void demoFgets() {
char line[25];
int n;
// open test file
SdFile file("FGETS.TXT", O_READ);
// check for open error
if (!file.isOpen()) error("demoFgets");
// read lines from the file
while ((n = file.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
// remove '\n'
line[n-1] = 0;
// replace next line with LCD call to display line
Serial.println(line);
} else {
// no '\n' - line too long or missing '\n' at EOF
// handle error
}
}
}
Hi robtillaart
Can you explain me how to use your function PLEASE?
I can't read each line of my text file on sd card
can you read one single char from a file?
Yeah using myFile.read()
Yeah using myFile.read()
Can you then tell if you read a carriage return? If not, store the value in the next position in an array, increment the position, and store a NULL terminator. If so, process the data in the array, reset the index. and store a NULL at the new index location.
Thank u PaulS
I've used this and it works well:
char val[20];
float v;
int i=0;
//...
while(val[i-1]!='\r'){
val[i]=myFile.read();
i++;
}
v=atoi(val);
//....the rest of program
I've used this and it works well:
I can't imagine how. Now, if you were incrementing i, instead of l, I might understand.
Sorry It's just typo
Please, I want to read data from the SD card, and stored it in a variable of type string for using it after in the rest of the program,
is that someone can help me
Please, I want to read data from the SD card, and stored it in a variable of type string for using it after in the rest of the program,
is that someone can help me
No. The Arduino doesn't have a type string. It has char arrays (which, when NULL terminated are referred to as strings) and it has Strings.
What have you tried? There are, at last count, roughly 14 bazillions examples of reading data one character at a time from a file, and storing the data in an array or String (ugh!).
.