Arduino File.readBytes()


The File.readBytes() function read characters from a file into a buffer. The function terminates if the determined length has been read, or it times out (see setTimeout()).

The File.readBytes() function returns the number of bytes placed in the buffer. A 0 means no valid data was found.

The File.readBytes() function inherits from the Stream utility class.


file.readBytes(buffer, length)

Parameter Values

  • file: an instance of a class that inherits from File.
  • buffer: the buffer to store the bytes in. Allowed data types: array of char or byte.
  • length: the number of bytes to read. Allowed data types: int.

Return Values

  • The number of bytes placed in the buffer. Data type: size_t.

Example Code

Create a arduino.txt file with the below content

Hi Arduino
/* * Created by * * This example code is in the public domain * * Tutorial page: */ #include <SD.h> #define PIN_SPI_CS 4 File file; char buf[20]; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); while (1); // don't do anything more: } // open file for reading file ="arduino.txt", FILE_READ); if (file) { int avail_len = file.available(); int read_len = file.readBytes(buf, avail_len); // read all to buffer to buffer Serial.print("Length:"); Serial.println(read_len); Serial.println("Content:"); Serial.println(buf); file.close(); } else { Serial.print(F("SD Card: error on opening file")); } } void loop() { }
  • Open Serial Monitor, you will see as below:
Length:10 Content: Hi Arduino
Autoscroll Show timestamp
Clear output
9600 baud  


Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.