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:
- The base (format) to be printed for integral data types (byte, char, int, long, short, unsigned char, unsigned int, unsigned long, word). The permitted values are:
- BIN: binary, or base 2
- OCT: octal, or base 8
- DEC: decimal, or base 10
- HEX: hexadecimal, or base 16
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:
- The base (format) to be printed for integral data types (byte, char, int, long, short, unsigned char, unsigned int, unsigned long, word). The permitted values are:
- BIN: binary, or base 2
- OCT: octal, or base 8
- DEC: decimal, or base 10
- HEX: hexadecimal, or base 16
Returns
The File.print() function returns the number of bytes written to the file.
Example Code
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
Tutorials
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables .