FileReadFields
The function FileReadFields reads values contained in the fields of a CSV file, and then it writes those values to a series of project tags or array elements.
Function | Group | Execution | Windows | Embedded | Thin Client | Mobile Access |
---|---|---|---|---|---|---|
FileReadFields | File | Synchronous | Supported | Supported | Not supported | Not supported |
Syntax
FileReadFields(strFilename,numOffset,strStartTagName,numNumberOfTags)
- strFilename
- The file path and/or name of the CSV file. If the file is located inside your project folder, you can specify either just the file name or the file path relative to that folder. If the file is located outside your project folder, you must specify the absolute file path.
- numOffset
- The number of bytes to skip in the CSV file before reading values. To read from the start of the file, numOffset should be 0.
You can use this parameter to start reading from any position in the file, as long as you know how many bytes to skip. In most cases, you will simply take the value returned by the previous execution of this function (see “Returned value” below) and use it to resume reading where you previously stopped.
However, if you already know the structure of the file and where you want to start in it, you can do that. For example, if you know that each line of the file is exactly 100 bytes and you want to read from the start of the fifth line, numOffset should be 400.
Note: The number of bytes per character in a file depends on the text encoding (i.e., ANSI, UTF-8, UTF-16, or other), the byte order, and the language or character set. - strStartTagName
- The name of the first project tag or array element in the series that will receive the values read from the CSV file. Project tags should be sequentially numbered with a numerical suffix, and the series is determined by incrementing that suffix (e.g., MyTag1, MyTag2, MyTag3, and so on). Array elements are handled similarly: the series is determined by incrementing the array index (e.g., MyArray[1], MyArray[2], MyArray[3], and so on). You do not need to begin the numbering with 1.
Note: If the tag name is not enclosed in quotes, the function will try to use the value of specified tag.
- numNumberOfTags
- The number of project tags or array elements in the series that will receive values read from the CSV file. For example, if strStartTagName is MyTag4 and numNumberOfTags is 5, five values will be read from the file and then written to the tags MyTag4, MyTag5, MyTag6, MyTag7, and MyTag8.
Returned value
If this function is successfully executed, it returns the position of the last byte read from the CSV file (including 0 if no bytes were read), which can be used in turn as the offset for the next batch of fields to be read.
If this function fails, it returns a negative value.
Notes
“CSV” is an abbreviation of “comma-separated values”, and in most cases, a CSV file is simply a plain text file that uses commas (,) to delimit its data fields. Only comma delimiters — as opposed to tabs, spaces, pipes, or other characters — are supported in CSV files. Each field in the file contains exactly one value, even if the value is empty, and each project tag or array element will receive exactly one value read from the file.
When this function is executed, it will read at most one line of data. If a line feed is found before reading the specified number of values, execution of the function will be aborted without reading any values.
Examples
FileReadFields("C:FieldFilesFieldFile01.csv",400,"IntValueTag003",5)
FileReadFields("FieldFile02.csv",0,"IntValueTag[0]",10)
FileReadFields("FieldFile03.csv",0,"IntValueTag[IndexTag]",7)