Arduino File.size()

Description

The File.size() function gets the size of the file.

Syntax

file.size()

Parameters

  • file: an instance of the File class that is returned by SD.open()

Returns

  • The size of the file in bytes (unsigned long)

Example Code

Create a arduino.txt file with the below content

Hi Arduino
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.size */ #include <SD.h> #define PIN_SPI_CS 4 File file; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { Serial.println("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) { Serial.print("File size: "); Serial.println(file.size()); } else { Serial.print("SD Card: error on opening file"); } } void loop() { }
  • Open Serial Monitor, you will see as below:
COM6
Send
File size: 10
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

ARDUINO BUY RECOMMENDATION

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.

※ OUR MESSAGES