Friday, May 27, 2011

File functions

File functions
FileClose ()
FileDelete ()
FileFlush ()
FileIsEnding ()
FileIsLineEnding ()
FileOpen ()
FileReadArray ()
FileReadDouble ()
FileReadInteger ()
FileReadNumber ()
FileReadString ()
FileSeek ()
Filesize ()
FileTell ()
FileWrite ()
FileWriteArray ()
FileWriteDouble ()
FileWriteInteger ()
FileWriteString ()


void FileClose (int handle)

Close the file previously opened by FileOpen () functions.
Parameters

handle - File handle, returned by FileOpen () functions
Sample
int handle = FileOpen ("filename", FILE_CSV | FILE_READ);
if (handle> 0)
{
/ / Working with files ...
FileClose (handle);
}


void FileDelete (string filename)

Remove the specified file name.
Note: Files can be erased only by terminal_dir \ experts \ files directory and it's subdirectories. Root directory for file operations is terminal_dir \ experts \ files directory.
Parameters

filename - The path to the file.
Sample
/ / File my_table.csv will be removed from terminal_dir \ experts \ files directory
FileDelete ("my_table.csv");


void FileFlush (int handle)

Flushes all data stored in the file buffer to disk.
Parameters

handle - File handle, returned by FileOpen () functions.
Sample
int bars_count = pubs;
int handle = FileOpen ("mydat.csv", FILE_CSV | FILE_WRITE);
if (handle> 0)
{
FileWrite (handle, "#"," OPEN "," Close "," high "," Low ");
for (int i = 0; i <bars_count; i + +)
FileWrite (handle, i +1, Open [i], near [i], high [i], Low [i]);
FileFlush (handle);
...
for (int i = 0; i <bars_count; i + +)
FileWrite (handle, i +1, Open [i], near [i], high [i], Low [i]);
FileClose (handle);
}


bool FileIsEnding (int handle)

Returns true if the logical file pointer at end of file, otherwise returns false.
Parameters

handle - File handle, returned by FileOpen () functions.
Sample
if (FileIsEnding (H1))
{
FileClose (H1);
return (false);
}


bool FileIsLineEnding (int handle)

The CSV file returns logical true if file pointer is at the end of the line, otherwise returns false.
Parameters

handle - File handle, returned by FileOpen () function.
Sample
if (FileIsLineEnding (H1))
{
FileClose (H1);
return (false);
}


int FileOpen (string filename, "int" mode, int delimiter =';')

Opens a file for input and / or output. Returns a file handle for open file. If the function fails, return value less than 0. To get extended error information, call GetLastError ().
Note: Files can be opened only by terminal_dir \ experts \ files and directories under it.
Parameters

filename - Name of file, file can be with all extensions.
mode - Open mode. may be one or a combination of values: FILE_BIN, FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - delimiter character in CSV files. By default pass ";" symbol.
Sample
int handle;
handle = FileOpen ("my_data.csv", FILE_CSV | FILE_READ ,";");
if (handle <1)
{
Print ("File not found my_data.dat last error is", GetLastError ());
return (false);
}


int FileReadArray (int handle, object and array [], int start, int count)

Reads count suggested elements of a binary file array. Returns true read elements count.
Note: Before reading the data array must be modified with sufficient size.
Parameters

handle - File handle, returned by FileOpen () function.
string [] - An array where the data will be saved.
Home - Store the starting position in the array.
count - Count of elements to read.
Sample
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)

Read number of binary files from the current file position. Number format size can be 8 bytes (double), or 4 bytes (float) length. If the format size is not a system trying to read 8 bytes long (double) value.
Parameters

handle - File handle, returned by FileOpen () function.
Size - Number format size. can DOUBLE_VALUE (8 bytes) or FLOAT_VALUE (4 bytes).
Sample
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)

Read the number of binary files from the current file position. Integer format size can be 1, 2 or 4 bytes long. If the format size is not a system trying to read 4 bytes length value.
Parameters

handle - File handle, returned by FileOpen () function.
Size - Format size. Can CHAR_VALUE (1 byte), SHORT_VALUE (2 bytes) or LONG_VALUE (4 bytes).
Sample
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)

Read the number from the current file position to delimiter. Only CSV files.
Parameters

handle - File handle, returned by FileOpen () function.
Sample
int handle;
int value;
handle = FileOpen ("filename.csv", FILE_CSV ", \ t");
if (handle> 0)
{
value = FileReadNumber (handle);
FileClose (handle);
}


array FileReadString (int handle, int length = 0)

Read the string from the current file position. Apply and CSV and binary files. The sequence text files will be read and the delimiter string for binary files will be read to indicate characters count.
Parameters

handle - File handle, returned by FileOpen () function.
Length - Read character count.
Sample
int handle;
string str;
handle = FileOpen ("filename.csv", FILE_CSV | FILE_READ);
str = FileReadString (handle);


void FileSeek (int handle, int offset, int origin)

Move the file pointer to a given location. The FileSeek () function moves the file pointer associated with handle for a new location that is offset bytes from origin. The next operation on the file appears on the new location.
Parameters

handle - File handle, returned by FileOpen () functions.
offset - Offset in bytes from origin.
origin - the starting position. Value can be one of the constants:
SEEK_CUR - the current position,
SEEK_SET - on top
SEEK_END - from the end of the file.
Sample
int handle = FileOpen ("filename.csv", FILE_CSV | FILE_READ, ";");
FileSeek (handle, 10, SEEK_SET);
if (FileReadIneger (handle)! = 0xFFFFFFFF)
{
FileClose (handle);
handle = 0;
return (0);
}


Filesize int (int handle)

Returns the file size in bytes.
Parameters

handle - File handle, returned by FileOpen () function.
Sample
int handle;
int size;
handle = FileOpen ("my_table.dat", FILE_BIN | FILE_READ);
Size = Size of file (handle);
Print ("my_table.dat size is" size "bytes");
FileClose (handle);


int FileTell (int handle)

Returns the current cursor position of the file.
Parameters

handle - File handle, returned by FileOpen () function.
Sample
int handle;
int pos;
handle = FileOpen ("my_table.dat", FILE_BIN | FILE_READ);
/ / Read some data
pos = FileTell (handle);
Print ("current position", pos);


int FileWrite (int handle, ...)

Writes a CSV file some values, delimiter added automatically. Returns number of characters written or a negative value if an error occurs.
Parameters

handle - File handle, returned by FileOpen () function.
... - Data for the user to write, separated by commas. Note: int, double types are automatically converted to string, but skin color, DateTime and bool type does not automatically converted and will be recorded in the file as integers.
Sample
int handle;
datetime orderOpen = OrderOpenTime ();
handle = FileOpen ("filename", FILE_CSV | FILE_WRITE ", \ t");
FileWrite (handle, Close [0], Open [0], high [0], Low [0], TimeToStr (orderOpen));


int FileWriteArray (int handle, object array [], int start, int count)

Writes a string of binary file. Arrays of type int, bool, DateTime, and color can be written in 4 bytes integer. Arrays of type double can be written as 8 bytes floating point numbers. Text strings can be written as a string where the items will be divided by the transport and return line feed characters (0D 0A).
Returns number of items written, or negative value if an error occurs.
Parameters

handle - File handle, returned by FileOpen () function.
string [] - String to write.
start - Starting index in the array to write.
count - Count of elements to write.
Sample
int handle;
double BarOpenValues ��[10];
for (int i = 0; i <10; i + +)
{
BarOpenValues ��[i] = Open [i];
}
handle = FileOpen ("mydata.dat", FILE_BIN | FILE_WRITE);
FileWriteArray (handle, BarOpenValues, 3, 7) / / write the last 7 items


int FileWriteDouble (int handle, double value, int size = DOUBLE_VALUE)

Writes a double value to a binary file. If size is FLOAT_VALUE, the value will be written int 4 bytes decimal format, others will be written in 8 bytes decimal format. Returns actual bytes written count.
Parameters

handle - File handle, returned by FileOpen () function.
value - value to write.
Size - Optional format flag. It may be one of the following values:
DOUBLE_VALUE (8 bytes, default)
or FLOAT_VALUE (4 bytes).
Sample
int h1;
double var1 = 0,345;
h1 = FileOpen ("mydata.dat", FILE_BIN | FILE_WRITE);
FileWriteDouble (H1, var1, DOUBLE_VALUE);


int FileWriteInteger (int handle, int value, int size = LONG_VALUE)

Writes an integer value with a binary file. If size is SHORT_VALUE, the value will be written as integer 2 bytes if the size is CHAR_VALUE, the value will be written as integer 1 byte if size is LONG_VALUE, the value will be written in 4 bytes number. Returns actual bytes written count.
Parameters

handle - File handle, returned by FileOpen () function.
value - value to write.
Size - Optional format flag. It may be one of the following values:
CHAR_VALUE (1 byte)
SHORT_VALUE (2 bytes)
LONG_VALUE (4 bytes by default).
Sample
int handle;
int value = 10;
handle = FileOpen ("filename.dat", FILE_BIN | FILE_WRITE);
FileWriteInteger (handle, value, SHORT_VALUE);


int FileWriteString (int handle, string value, length int)

Writes a sequence of binary files from the current position.
Note: CR / LF symbols are listed in the file. Returns actual bytes written count.
Parameters

handle - File handle, returned by FileOpen () function.
value - text to write.
Length - Counts of characters to write.
Sample
int h1;
string str = "some string";
h1 = FileOpen ("filename.bin", FILE_BIN | FILE_WRITE);
FileWriteString (H1, st, 8);

No comments:

Post a Comment

Powered by Blogger.