2

I have some C++ code, but I cant convert it to Arduino language. I just want to 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(and this line will delete in text 
             file... but that is another issue... i will just focus to get the 
             line1 and get the value)

line2

line3

line4

line5

This code displays all lines

    #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);

       // 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
     }
1
  • 1
    You'll be happy to learn that there is no Arduino language. Arduino's .ino files are C++! The only difference is the Arduino IDE automatically generates function prototypes if you don't provide them yourself, but that doesn't make it a different language.
    – per1234
    Commented Aug 30, 2018 at 5:28

2 Answers 2

2

There is no builtin way to do that but you got all the necessary tools at hand.

End of line should be signaled by a decimal 10, aka LF(Line Feed) character. So what you do is keep reading until you encounter LF or end of file, which then is the entire line you want to read.

while (file.available() && file.peek() != 10) // peek returns the next character without incrementing the read index
    file.read();

This code does exactly that. Now you could either store the read bytes in a buffer (big enough!) right away or instead find out the number of bytes beforehand to create a precisely sized buffer afterwards and use the read(buffer, length) overload to read all bytes at once.

Please keep in mind that the LF character has to be there in order to be read. So if you are the one creating the files, be sure to place it. (most desktop text editors do that for you)

Example:

char buffer[20];
uint8_t i = 0;

while (file.available() && file.peek() != '\n' && i < 19)    // note how this also prevents the buffer from overflowing (18 max to leave space for '\0'!)
{
    buffer[i] = file.read();
    i++;
}
buffer[i + 1] = '\0';    // it is good practice to end strings with a terminating NULL character
4
  • i understand what you are saying but im sorry @mystery, i dont even know what function should i used :( im just new in arduino...
    – awesome23
    Commented Aug 29, 2018 at 16:36
  • Should it not be " i < 18" instead of "i < 19"? If i is 18 it will become 19 with the i++. Then buffer[i+1] will be buffer[20] which will fail.
    – Noel
    Commented Jul 11 at 14:57
  • it also needs a file.read() after the while loop. Otherwise the \n never gets read and next line being read will be stuck on that \n.
    – Noel
    Commented Jul 11 at 15:49
  • and it should be buffer[i] = '\0' in the end.
    – Noel
    Commented Jul 11 at 15:50
-1

If I understand you correctly, it depends on what you want to do with that code. Do you want to execute lines of C++ code in/on the SD card or are you merely attempting to convert what you have in C++ to "Arduinoese?"

If you need to, you can actually execute microcontroler-based C/C++ code on your arduino if it has an AVR in it, and you have the proper AVR Toolchain.

Elliot William's "Make: AVR Programming" book is a great place to learn about this.

1
  • You can run C/C++ on any Arduino board, not only the AVR ones, and if you can use the board with the Arduino IDE then you already have the toolchain. This is certain because Arduino sketches are C++.
    – per1234
    Commented Aug 30, 2018 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.