tsc_File

This class performs a number of file-related functions, including some basic text file reading and writing methods.  Note that because this API was once required to work with Windows CE, there is no concept of a current directory, and relative paths will fail to work when applied to the filesystem.

Those functions which have a bool return type and aren't returning some property will return true if the operation succeeded, or false otherwise.

Member functions

static unsigned int GetLastError ();
This method returns the last error from any of the functions in this class.  It does not reflect the result of methods in any other class or API.  The returned value is zero if no error occurred, or a Windows error number, obtained from the Windows GetLastError() API call.

static bool Exists (const char* path);
Returns true if the specified file exists.

static bool Delete (const char* path);
Deletes the specified file.      

static bool Move (const char* fromPath, const char* newName);
Moves or renames the file.  If newName contains just a filename and extension then the file is renamed.  If newName specifies a different directory, the file is moved there and optionally renamed at the same time.

static bool Copy (const char* fromPath, const char* toPath);
Copies the file to the new path, a rename can occur if the file names are different.  Both path names must contain a file name.  If the destination file already exists, it will be overwritten.

static bool WildcardMatch (const char* fileName, const char* fileMask);
Tests if a filename matches a file mask which may include the ? and * wildcard characters.  Avoid passing directory information in either parameter.

For example:

if (!tsc_File::WildcardMatch(fileName, "*.job"))

{

    tsc_MessageBox::Show(X_Error, PX_YouMustSelectAJob);

}

static tsc_String GetTemporaryFileName ();
Generate a unique (at the time of request) filename in the temporary directory of the operating system.

static bool WriteAllText (const char* filePath, const char* text, bool overwrite = false);
Creates the file given by filePath, and stores the contents of the supplied string into the file. If overwrite is false and the file already exists, the function fails.  If overwrite is true and the file exists, the existing file is deleted first.  The text is written as one byte per character and therefore supports only UTF-8 encoding. A null character must terminate the text string. No BOM mark is written, although it could be supplied at the start of the text string.

static bool WriteAllLines (const char* filePath, const tsc_StringList lines, bool overwrite = false);
Writes all the supplied strings, one string per line, into the given file. The text is written as one byte per character and therefore supports UTF-8 encoding. No BOM mark is written. If overwrite is true then any existing file is deleted first. If overwrite is false and the file already exists, the function fails.  The line end characters "\r\n" are appended to every line.

static tsc_String ReadAllText (const char* filePath);
Reads the text from the given UTF-8 file into a string. If the file includes a UTF-8 BOM mark, the mark is discarded.  If the operation fails the returned string will be empty and the GetLastError member of tsc_File will return an error code other than zero.

Note: If the file contains null characters the string may behave unpredictably.

static tsc_StringList ReadAllLines (const char* filePath);
Reads the text from the file into a list of strings, one string per line in the file. 

If the operation fails the returned string list will be empty and the GetLastError member of tsc_File will return an error code other than zero.

Starting with version 2022.00
Lines are delimited by a single \n or \r, or by \r\n. The file is first examined for encoding clues, including the presence of a BOM mark or two-byte characters, and if necessary is converted into utf-8 as required for the tsc_String / tsc_StringList classes. ReadLine supports these file encodings: ASCII, utf-8, and utf-16 with either endianness. The older Windows ucs-2 (16 bit wchar) format is also supported since it is a subset of utf-16.

If the file contains null characters (8 or 16 bit zeros), the lines containing them will be truncated at the null character position and the rest of the line will be discarded. 

Prior to version 2022.00

Lines are delimited by \n characters, and \r characters are ignored and discarded. Any UTF-8 BOM mark at the start of the file will be discarded. Only ASCII and utf-8 formats are supported.