Arduino File.print()

Description

The File.print() function prints data to the file, which must have been opened for writing. The File.print() function accepts many type of data.(int, float, char, String ....) For example:

  • file.print(14) prints "14" to the file
  • file.print(1.23415) prints "1.23" to the file
  • file.print('N') prints "N" to the file
  • file.print("Arduino") prints "Arduino" to the file

An optional second parameter specifies:

For example:

  • file.print(14, BIN) gives "1001110"
  • file.print(14, OCT) gives "116"
  • file.print(14, DEC) gives "14"
  • file.print(14, HEX) gives "4E"
  • file.print(1.23415, 0) gives "1"
  • file.print(1.23415, 2) gives "1.23"
  • file.print(1.23415, 4) gives "1.2341"

You can pass flash-memory based strings to file.print() by wrapping them with F(). For example:

file.print(F("Arduino"))

To send data without conversion to its representation as characters, use File.write().

The print.() function inherits from the Stream utility class.

Syntax

file.print(data)

file.print(data, format)

Parameter Values

  • File: an instance of the File class that is returned by SD.open()
  • Data: the data to print (char, byte, int, long, or string)
  • format: (optional) specifies:

Returns

The File.print() function returns the number of bytes written to the file.

Example Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.print */ #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: } SD.remove("arduino.txt"); // delete the file if existed // create new file by opening file for writing file = SD.open("arduino.txt", FILE_WRITE); if (file) { // prints character file.print('A'); file.print('R'); file.print('D'); file.print('U'); file.print('I'); file.print('N'); file.print('O'); file.print('\n'); // prints a new line character file.print('\n'); // prints a new line character // prints a string file.print("ArduinoGetStarted.com"); file.print("\n\n"); // prints a string with two new line characters // prints a floating point number float a = 1.23456; file.print(a); // prints 2 decimal places by default file.print('\n'); // prints a new line character file.print(a, 4); // prints 4 decimal places file.print('\n'); // prints a new line character file.print(a, 5); // prints 5 decimal places file.print('\n'); // prints a new line character file.print(a, 6); // prints 6 decimal places file.print("\n\n"); // prints a string with two new line characters int x = 77; // prints an integral number in diffrent format in a table file.print("DEFAULT"); // prints a string file.print('\t'); // prints a tab character file.print(x); // prints as an ASCII-encoded decimal by default - same as "DEC" file.print('\n'); // prints a new line character file.print("DEC"); // prints a string file.print('\t'); // prints a tab character file.print(x, DEC); // prints as an ASCII-encoded decimal file.print('\n'); // prints a new line character file.print("HEX"); // prints a string file.print('\t'); file.print(x, HEX); // prints as an ASCII-encoded hexadecimal file.print('\n'); // prints a new line character file.print("OCT"); // prints a string file.print('\t'); // prints a tab character file.print(x, OCT); // prints as an ASCII-encoded octal file.print('\n'); // prints a new line character file.print("BIN"); // prints a string file.print('\t'); // prints a tab character file.print(x, BIN); // prints as an ASCII-encoded binary file.close(); } else { Serial.print("SD Card: error on opening file arduino.txt"); } } void loop() { }

After running the above code, if you disconnect SD Cart from Arduino and connect the SD Card to your your PC, you will see a arduino.txt file with the below content

ARDUINO ArduinoGetStarted.com 1.23 1.2346 1.23456 1.234560 DEFAULT 77 DEC 77 HEX 4D OCT 115 BIN 1001101

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