FileReadString
FileReadString
此函数从文件指针的当前位置读取字符串。
string FileReadString(
int file_handle, // File handle
int length=0 // Length of the string
);参数
- file_handle
[in] 由 FileOpen() 返回的文件描述符。
- length=0
[in] 要读取的字符数。
返回值
读取的行(字符串)。
注意
当从二进制文件读取时,必须指定要读取的字符串长度。当从文本文件读取时,不需要指定字符串长度,字符串将从当前位置读到换行符字符“\r\n”。当从CSV文件读取时,也不需要指定字符串长度,字符串将从当前位置读到最近的分隔符或文本字符串的结尾字符。
如果文件使用 FILE_ANSI 标志打开,则读取的行将被转换为Unicode格式。
示例(此处使用了执行 FileWriteInteger() 函数后得到的文件)
//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for data reading
input string InpFileName="Trend.bin"; // file name
input string InpDirectoryName="Data"; // directory name
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- open the file
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- additional variables
int str_size;
string str;
//--- read data from the file
while(!FileIsEnding(file_handle))
{
//--- find out how many symbols are used for writing the time
str_size=FileReadInteger(file_handle,INT_VALUE);
//--- read the string
str=FileReadString(file_handle,str_size);
//--- print the string
PrintFormat(str);
}
//--- close the file
FileClose(file_handle);
PrintFormat("Data is read, %s file is closed",InpFileName);
}
else
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
}另请参阅
最后更新于