ASL
TextFile Class Reference

Detailed Description

Class TextFile represents a text file in the filesystem.

It can be used to get information about a file (its size, its modification date, whether it exists), to read or write in it in one step, or to open it and perform typical read/write operations as text, just like File.

Functions text(),lines(),write(),append(),printf()`, etc. read or write in the file by first opening it. Intended as a short-hand for one-line operations.

TextFile("info.log").printf("Error connecting to port %i\n", port);
String text = TextFile("data.txt").text();
TextFile("data.json").write(Json::encode(data));
String last_entry = TextFile("errors.log").lines().last();
String text()
Returns the textual content of the file as a string.
static String encode(const Var &v, Mode mode=NONE)
Encodes the given Var into a JSON-format representation.

Explicit opening allows reading or writing in an stdio conventional style:

TextFile log("info.log", File::APPEND);
log.printf("There are %i new messages\n", n_messages);
log << "App exited with code " << exitCode << '\n';
TextFile log("http-access.log", File::READ)
while(!log.end())
{
String line = log.readLine();
...
}
void log(const String &cat, Log::Level level, const String &message)

#include <TextFile.h>

Inheritance diagram for TextFile:
File

Public Member Functions

bool printf (const char *fmt,...) ASL_PRINTF_W2(2)
 Prints formatted text as with the regular printf. More...
 
int scanf (const String &fmt, void *p1, void *p2=0, void *p3=0, void *p4=0)
 Reads formatted text as with the regular scanf up to 4 items only!
 
String readLine ()
 Reads and returns a line from the file.
 
String text ()
 Returns the textual content of the file as a string. More...
 
Array< Stringlines ()
 Returns all the lines contained in the file as an array of strings. More...
 
bool append (const String &line)
 Writes the given string at the end of the file, opening in append mode if the file was not opened. More...
 
bool write (const String &s)
 Writes the given string at the end of the file, opening in write mode if the file was not opened. More...
 
bool put (const String &s)
 Writes the given string at the end of the file, opening in write mode if the file was not opened. More...
 
- Public Member Functions inherited from File
 File ()
 Constructs a File object with no associated file.
 
 File (const String &name)
 Constructs a File object with the given path name but does not open it.
 
 File (const String &name, OpenMode mode)
 Constructs a File object with the given path name and opens it with the given access mode. More...
 
 operator bool () const
 Returns true if this object refers to an open file.
 
bool operator== (const File &f) const
 Returns true if this object does not refer to an open file.
 
Date lastModified () const
 Returns the file's last modification date.
 
Date creationDate () const
 Returns the file's creation date.
 
bool setLastModified (const Date &t)
 Sets the file's last modification date.
 
Long size () const
 Returns the file size.
 
String name () const
 Returns the file's name (without its directory)
 
String extension () const
 Returns the file's extension (what follows the last dot)
 
bool hasExtension (const String &exts) const
 Returns true if this file's extension is any of those given (separated by '|'), case-insensitively.
 
const Stringpath () const
 Returns the full path of the file.
 
String directory () const
 Returns the directory containing the file.
 
bool isDirectory () const
 Returns true if the 'file' path is a directory.
 
bool exists () const
 Returns true if the 'file' path exists (file or directory)
 
bool isFile () const
 Returns true if the 'file' path exists and is actually a file.
 
bool copy (const String &to)
 Copies this file to a new directory or name.
 
bool move (const String &to)
 Moves or renames this file to to which can be a full name or a destination directory.
 
bool remove ()
 Deletes the file.
 
ByteArray firstBytes (int n)
 Returns the first n bytes in the file.
 
ByteArray content ()
 Returns the binary content of the file as an array of bytes.
 
bool put (const ByteArray &data)
 Writes the binary content of the file from an array of bytes. More...
 
bool open (const String &name, OpenMode mode=READ)
 Opens the file with the specified access mode.
 
bool open (OpenMode mode=READ)
 Opens the file with the specified access mode.
 
void close ()
 Closes the file.
 
Long position ()
 Returns the current position in the file.
 
void seek (Long offset, SeekMode from=START)
 Moves the file pointer to the given position. More...
 
bool end ()
 Returns true if the file pointer reached the end of the file.
 
void flush ()
 Flushes the write buffers effectively writing data on disk.
 
int read (void *p, int n)
 Reads n bytes from the file into the buffer pointed to by p.
 
int write (const void *p, int n)
 Writes n bytes from the buffer pointed to by p into the file.
 
template<class T >
Fileoperator<< (const T &x)
 Writes variable x to the file respecting endianness in binary form.
 
template<class T >
Fileoperator>> (T &x)
 Reads variable x from the file respecting endianness in binary form.
 

Additional Inherited Members

- Static Public Member Functions inherited from File
static File temp (const String &ext=".tmp")
 Creates a temporary file with optional extension and returns its path.
 

Member Function Documentation

◆ append()

bool append ( const String line)

Writes the given string at the end of the file, opening in append mode if the file was not opened.

Returns false on failure.

◆ lines()

Array<String> lines ( )

Returns all the lines contained in the file as an array of strings.

The file does not need to be opened.

◆ printf()

bool printf ( const char *  fmt,
  ... 
)

Prints formatted text as with the regular printf.

Returns false on failure

◆ put()

bool put ( const String s)
inline

Writes the given string at the end of the file, opening in write mode if the file was not opened.

Returns false on failure.

◆ text()

String text ( )

Returns the textual content of the file as a string.

The file does not need to be opened.

◆ write()

bool write ( const String s)

Writes the given string at the end of the file, opening in write mode if the file was not opened.

Returns false on failure.


The documentation for this class was generated from the following file: