fileToString()

This function loads an ASCII file and writes the contents into a string variable.

Synopsis

bool fileToString(string fileName, string &result [, string encoding]);

Parameters

Parameter Meaning
fileName Name of the file. If the path name is relative, the file name is interpreted as a CTRL script, that is, the function searches for it in <proj_path>/script.
&result Contents of the file.
encoding

The encoding. See chapter recode() for a list of possible encodings.

As of the version 3.16 ISO projects cannot be created anymore. Create UTF-8 projects instead.

Return Value

In the case of an error, the function returns FALSE.

Errors

Missing arguments or file does not exist.

Description

The function loads an ASCII file and writes the contents into a string variable.

To use this function for text files encoded with UTF-8-BOM (Byte Order Mark) the parameter "encoding" must to be set to "UTF8".

Example

main()
{
  string result;
  bool x;
  x = fileToString("test.ctl", result);
  DebugN(result);
  //Output of the content of "test.ctl" in the Log viewer
}
main()
{
  string templateFile;

/* An ISO file - fileToString().
    Writes the content of the ISO file to a string */
  bool ret = fileToString("D:\\Documents\\recodeISO.txt", templateFile, "ISO88591");
  DebugN("fileToString() return", ret);
  dyn_string templateConfigs = strsplit(templateFile, '\n');
  DebugN("An ISO file to String",templateConfigs);

//ISO file - recode() function. Converts an ISO file to a UTF8 file
  bool ret2 = fileToString("D:\\Documents\\recodeISO.txt", templateFile);
  DebugN("fileToString() return ", ret2);
  templateFile = recode(templateFile, "ISO88591", "UTF8");
  dyn_string templateConfigs = strsplit(templateFile, '\n');
  DebugN("An ISO String to UTF",templateConfigs);
}

Assignment

File functions

Availability

CTRL