文件處理函數 [File Functions]
void FileClose(int handle)
關閉正在已經打開的文件.
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
int handle=FileOpen("filename", FILE_CSV|FILE_READ);
if(handle>0)
{
// working with file ...
FileClose(handle);
}
void FileDelete(string filename)
刪除文件,如果發生錯誤可以通過GetLastError()來查詢
注:你只能操作terminal_dir\experts\files目錄下的文件
:: 輸入參數
filename - 目錄和文件名
示例:
// file my_table.csv will be deleted from terminal_dir\experts\files directory
int lastError;
FileDelete("my_table.csv");
lastError=GetLastError();
if(laseError!=ERR_NOERROR)
{
Print("An error ocurred while (",lastError,") deleting file my_table.csv");
return(0);
}
void FileFlush(int handle)
將緩存中的數據刷新到磁盤上去
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
if(handle>0)
{
FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
for(int i=0;i
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileFlush(handle);
...
for(int i=0;i
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileClose(handle);
}
bool FileIsEnding(int handle)
檢查是否到了文件尾.
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
if(FileIsEnding(h1))
{
FileClose(h1);
return(false);
}
bool FileIsLineEnding( int handle)
檢查行是否到了結束
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
if(FileIsLineEnding(h1))
{
FileClose(h1);
return(false);
}
int FileOpen( string filename, int mode, int delimiter=';')
打開文件,如果失敗返回值小於1,可以通過GetLastError()獲取錯誤
注:只能操作terminal_dir\experts\files目錄的文件
:: 輸入參數
filename - 目錄文件名
mode - 打開模式 FILE_BIN, FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV型打開模式用的分割符,默認為分號(;).
示例:
int handle;
handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ,';');
if(handle<1)
{
Print("File my_data.dat not found, the last error is ", GetLastError());
return(false);
}
int FileOpenHistory( string filename, int mode, int delimiter=';')
打開歷史數據文件,如果失敗返回值小於1,可以通過GetLastError()獲取錯誤
:: 輸入參數
filename - 目錄文件名
mode - 打開模式 FILE_BIN, FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV型打開模式用的分割符,默認為分號(;).
示例:
int handle=FileOpenHistory("USDX240.HST",FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("Cannot create file USDX240.HST");
return(false);
}
// work with file
// ...
FileClose(handle);
int FileReadArray( int handle, object& array[], int start, int count)
將二進制文件讀取到數組中,返回讀取的條數,可以通過GetLastError()獲取錯誤
注:在讀取之前要調整好數組大小
:: 輸入參數
handle - FileOpen()返回的句柄
array[] - 寫入的數組
start - 在數組中存儲的開始點
count - 讀取多少個對象
示例:
int handle;
double varray[10];
handle=FileOpen("filename.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
FileReadArray(handle, varray, 0, 10);
FileClose(handle);
}
double FileReadDouble( int handle, int size=DOUBLE_VALUE)
從文件中讀取浮點型數據,數字可以是8byte的double型或者是4byte的float型。
:: 輸入參數
handle - FileOpen()返回的句柄
size - 數字個是大小,DOUBLE_VALUE(8 bytes) 或者 FLOAT_VALUE(4 bytes).
示例:
int handle;
double value;
handle=FileOpen("mydata.dat",FILE_BIN);
if(handle>0)
{
value=FileReadDouble(handle,DOUBLE_VALUE);
FileClose(handle);
}
int FileReadInteger( int handle, int size=LONG_VALUE)
從文件中讀取整形型數據,數字可以是1,2,4byte的長度
:: 輸入參數
handle - FileOpen()返回的句柄
size - 數字個是大小,CHAR_VALUE(1 byte), SHORT_VALUE(2 bytes) 或者 LONG_VALUE(4 bytes).
示例:
int handle;
int value;
handle=FileOpen("mydata.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
value=FileReadInteger(h1,2);
FileClose(handle);
}
double FileReadNumber( int handle)
從文件中讀取數字,只能在CSV裡使用
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
int handle;
int value;
handle=FileOpen("filename.csv", FILE_CSV, ';');
if(handle>0)
{
value=FileReadNumber(handle);
FileClose(handle);
}
string FileReadString( int handle, int length=0)
從文件中讀取字符串
:: 輸入參數
handle - FileOpen()返回的句柄
length - 讀取字符串長度
示例:
int handle;
string str;
handle=FileOpen("filename.csv", FILE_CSV|FILE_READ);
if(handle>0)
{
str=FileReadString(handle);
FileClose(handle);
}
bool FileSeek( int handle, int offset, int origin)
移動指針移動到某一點,如果成功返回true
:: 輸入參數
handle - FileOpen()返回的句柄
offset - 設置的原點
origin - SEEK_CUR從當前位置開始 SEEK_SET從文件頭部開始 SEEK_END 從文件尾部開始
示例:
int handle=FileOpen("filename.csv", FILE_CSV|FILE_READ, ';');
if(handle>0)
{
FileSeek(handle, 10, SEEK_SET);
FileReadInteger(handle);
FileClose(handle);
handle=0;
}
int FileSize( int handle)
返回文件大小
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
int handle;
int size;
handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
size=FileSize(handle);
Print("my_table.dat size is ", size, " bytes");
FileClose(handle);
}
int FileTell( int handle)
返回文件讀寫指針當前的位置
:: 輸入參數
handle - FileOpen()返回的句柄
示例:
int handle;
int pos;
handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
// reading some data
pos=FileTell(handle);
Print("current position is ", pos);
int FileWrite( int handle, ... )
向文件寫入數據
::
輸入參數handle - FileOpen()返回的句柄
... - 寫入的數據
示例:
int handle;
datetime orderOpen=OrderOpenTime();
handle=FileOpen("filename", FILE_CSV|FILE_WRITE, ';');
if(handle>0)
{
FileWrite(handle, Close[0], Open[0], High[0], Low[0], TimeToStr(orderOpen));
FileClose(handle);
}
int FileWriteArray( int handle, object array[], int start, int count)
向文件寫入數組
:: 輸入參數
handle - FileOpen()返回的句柄
array[] - 要寫入的數組
start - 寫入的開始點
count - 寫入的項目數
示例:
int handle;
double BarOpenValues[10];
// copy first ten bars to the array
for(int i=0;i<10; i++)
BarOpenValues[i]=Open[i];
// writing array to the file
handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
if(handle>0)
{
FileWriteArray(handle, BarOpenValues, 3, 7); // writing last 7 elements
FileClose(handle);
}
int FileWriteDouble( int handle, double value, int size=DOUBLE_VALUE)
向文件寫入浮點型數據
:: 輸入參數
handle - FileOpen()返回的句柄
value - 要寫入的值
size - 寫入的格式,DOUBLE_VALUE (8 bytes, default)或FLOAT_VALUE (4 bytes).
示例:
int handle;
double var1=0.345;
handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteDouble(h1, var1, DOUBLE_VALUE);
//...
FileClose(handle);
int FileWriteInteger( int handle, int value, int size=LONG_VALUE)
向文件寫入整型數據
:: 輸入參數
handle - FileOpen()返回的句柄
value - 要寫入的值
size - 寫入的格式,CHAR_VALUE (1 byte),SHORT_VALUE (2 bytes),LONG_VALUE (4 bytes, default).
示例:
int handle;
int value=10;
handle=FileOpen("filename.dat", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteInteger(handle, value, SHORT_VALUE);
//...
FileClose(handle);
int FileWriteString( int handle, string value, int length)
向文件寫入字符串數據
:: 輸入參數
handle - FileOpen()返回的句柄
value - 要寫入的值
length - 寫入的字符長度
示例:
int handle;
string str="some string";
handle=FileOpen("filename.bin", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteString(h1, str, 8);
FileClose(handle);
微博:
閱讀更多 大侃金融 的文章