Arduino File.println()
Description
Prints data to a file on SD Card as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
File.println() is the same as File.print(), except for one thing: File.println() prints '\r' and '\n' character at the end while File.print() does not.
The File.println() function inherits from the Stream utility class.
Syntax
file.println(data)
file.println(data, format)
Parameter Values
- File: an instance of the File class that is returned by SD.open().
- data: the value to print. Allowed data types: any data type.
- 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
Return Values
- println() returns the number of bytes written to the file. Data type: size_t.
Example Code
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.println
*/
#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:
}
Serial.println("SD CARD INITIALIZED.");
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) {
// print a character
file.println('N');
// print a string
file.println(); // print an empty line
file.println("ArduinoGetStarted.com");
// print a float number
float a = 1.23456;
file.println(); // print an empty line
file.println(a); // 2 decimal places by default
file.println(a, 4); // 4 decimal places
file.println(a, 5); // 5 decimal places
file.println(a, 6); // 6 decimal places
// print an interger number out in many formats:
int x = 77;
file.println(); // print an empty line
file.println(x); // print as an ASCII-encoded decimal by default
file.println(x, DEC); // print as an ASCII-encoded decimal
file.println(x, HEX); // print as an ASCII-encoded hexadecimal
file.println(x, OCT); // print as an ASCII-encoded octal
file.println(x, BIN); // print as an ASCII-encoded binary
file.close();
} else {
Serial.print("SD Card: error on opening file arduino.txt");
}
}
void loop() {
}
The content of arduino.txt file:
N
ArduinoGetStarted.com
1.23
1.2346
1.23456
1.234560
77
77
4D
115
1001101
Tutorials
See Also
※ 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.