Arduino File.available()
Description
The File.available() function checks if there are any bytes available for reading from the file. The File.available() function inherits from the Stream utility class.
The File.available() function inherits from the Stream utility class.
Syntax
file.available()
Parameters
- file: an instance of the File class that is returned by SD.open()
Returns
- The number of bytes available ro read. The returned type: int
Example Code
- Connect the SD Card to your your PC via an USB 3.0 SD Card Reader
Create a arduino.txt file with the below content
Hi
Arduino
- Connect SD Card to Arduino. See Arduino - Micro SD Card tutorial
- Upload below code to Arduino via Arduino IDE
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.available
*/
#include <SD.h>
#define PIN_SPI_CS 4
File file;
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 = SD.open("arduino.txt", FILE_READ);
if (file) {
int length = file.available();
Serial.print("Bytes available: ");
Serial.println(length);
file.close();
} else {
Serial.print(F("SD Card: error on opening file"));
}
}
void loop() {
}
- Open Serial Monitor, you will see as below:
COM6
Bytes available: 10
Autoscroll
Clear output
9600 baud
Newline
Tutorials
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.
Additionally, some links direct to products from our own brand, DIYables.