what function I will use to grab only line1 from text file?
#include <SD.h>
#include <SPI.h>
File myFile;
void setup() {
Serial.begin(9600);
SD.begin();
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
// Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
// Serial.write(myFile.read());
char lineOne[80]; // Assumes that a line contains no more than 79 characters
byte index = 0;
while(myFile.available() > 0)
{
char nextChar = myFile.read();
if(nextChar != '\n')
{
lineOne[index++] = nextChar;
lineOne[index] = '\0'; // Keep array NULL terminated
}
else
{
//Serial.print("Line one: ");
Serial.println(lineOne);
break;
}
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
}
system
August 28, 2018, 11:32am
2
what function I will use to grab only line1 from text file?
File::read(), called as many times as needed, until you get the still-undefined end of line marker. There is NO read-a-complete-line-and-i'm-not-going-to-define-exactly-what-that-means() method. YOU must write that functionality.
That code does something. You have NOT shown us what it does.
You expect that code to do something. You have NOT been anywhere near clear on what you expect it to do.
J-M-L
August 28, 2018, 11:32am
3
what's the actual question ?
What does your code do - especially this:
while(myFile.available() > 0)
{
char nextChar = myFile.read();
if(nextChar != '\n')
{
lineOne[index++] = nextChar;
lineOne[index] = '\0'; // Keep array NULL terminated
}
else
{
//Serial.print("Line one: ");
Serial.println(lineOne);
break;
}
}
// read from the file until there's nothing else in it:
while (myFile.available()) {
// Serial.write(myFile.read());
This causes the program to do just what the comment says. The program will read all of the lines, not just one.
system
August 28, 2018, 11:48am
5
groundFungus:
// read from the file until there's nothing else in it:
while (myFile.available()) {
// Serial.write(myFile.read());
This causes the program to do just what the comment says. The program will read all of the lines, not just one.
Except that there is a break statement in the while loop.
while (myFile.available())
{
// Serial.write(myFile.read());
char lineOne[80]; // Assumes that a line contains no more than 79 characters
byte index = 0;
while (myFile.available() > 0)
{
char nextChar = myFile.read();
if (nextChar != '\n')
{
lineOne[index++] = nextChar;
lineOne[index] = '\0'; // Keep array NULL terminated
}
else
{
//Serial.print("Line one: ");
Serial.println(lineOne);
break;
}
}
}
Doesn't that break from the second while, passing control back to the first while?
system
August 28, 2018, 12:05pm
7
groundFungus:
while (myFile.available())
{
// Serial.write(myFile.read());
char lineOne[80]; // Assumes that a line contains no more than 79 characters
byte index = 0;
while (myFile.available() > 0)
{
char nextChar = myFile.read();
if (nextChar != '\n')
{
lineOne[index++] = nextChar;
lineOne[index] = '\0'; // Keep array NULL terminated
}
else
{
//Serial.print ("Line one: ");
Serial.println(lineOne);
break;
}
}
}
Doesn't that break from the second while, passing control back to the first while?
I missed that there were two while loops. There were not two of them when I wrote the snippet to read the first line, in another thread by the same poster.