i have a c++ code, but i cant convert it to arduino language..
i just wanna ask, what is the same as getline()(c++ code) function in arduino?
i just wanted to get the first line in my SD card...
i can only display all text inside my SD card...
example:
line1 <- only this line should display...
line2
line3
line4
line5
this code displays all line
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// 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());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop() {
// nothing happens after setup
}
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
If that is not what you want to do, why did you write the code that way?
Be VERY specific. What should stop the reading from the file? "The end of the line" is NOT specific. "The first carriage return" is. So, write some code that determines if the character you read, and stored, is a carriage return, BEFORE you print it.
You can work like this:
You have a global array that has to be surely long enought to store a line. All the lines terminates witch a specific char.
Your readlinefile function has got the number of the needed line and the array.
The function open the file and read it from the beginning. Every tine it founds an and line it add one to a variable. When the variable and the needed line ne are egual the function starts putting what it reads into the array, while it doesn't find a andline. In that moment function finished.
Is it ok?
Silente:
You can work like this:
You have a global array that has to be surely long enought to store a line. All the lines terminates witch a specific char.
Your readlinefile function has got the number of the needed line and the array.
The function open the file and read it from the beginning. Every tine it founds an and line it add one to a variable. When the variable and the needed line ne are egual the function starts putting what it reads into the array, while it doesn't find a andline. In that moment function finished.
Is it ok?
Which would be simple enough to do based on the program snippet that reads all the lines posted above. That program snippet detects the end of each line. It would be easy enough to add an if statement to only print the line when the line_count == selected_line.
The OP may be asking how to read the Nth line without reading lines 1 to N-1. I don't know if SDFat has a "seek to line" function. If not, then this would be the alternative.
I think OP asked how to SHOW only the Nth line, and it is different from READ.
Anyways you can READ only the Nth line only if the lines has got the same size, so you can cakvolate the first byte like N*size, and read while you aren't in (N+1)*size byre