open_fileT_open_fileOpenFileOpenFileopen_file (算子)
名称
open_fileT_open_fileOpenFileOpenFileopen_file — 以文本或二进制格式打开文件。
签名
void OpenFile(const HTuple& FileName, const HTuple& FileType, HTuple* FileHandle)
void HFile::HFile(const HString& FileName, const HTuple& FileType)
void HFile::HFile(const HString& FileName, const HString& FileType)
void HFile::HFile(const char* FileName, const char* FileType)
void HFile::HFile(const wchar_t* FileName, const wchar_t* FileType)
(
Windows only)
void HFile::OpenFile(const HString& FileName, const HTuple& FileType)
void HFile::OpenFile(const HString& FileName, const HString& FileType)
void HFile::OpenFile(const char* FileName, const char* FileType)
void HFile::OpenFile(const wchar_t* FileName, const wchar_t* FileType)
(
Windows only)
描述
open_fileopen_fileOpenFileOpenFileOpenFileopen_file opens a file in text format or in binary format.
The name of the file is defined by the parameter FileNameFileNameFileNameFileNamefileNamefile_name.
The operator open_fileopen_fileOpenFileOpenFileOpenFileopen_file returns a handle to the file in the output
parameter FileHandleFileHandleFileHandleFileHandlefileHandlefile_handle.
The parameter FileTypeFileTypeFileTypeFileTypefileTypefile_type determines the type of the file.
For text files this parameter can also be used to specify the encoding
used for strings in that file, where besides UTF-8 only the local-8-bit
encoding is supported.
The following settings for FileTypeFileTypeFileTypeFileTypefileTypefile_type are supported:
- 'input'"input""input""input""input""input":
-
An already existing input file is opened for reading in text format.
- 'output'"output""output""output""output""output":
-
A new output file is opened for writing in text format.
- 'append'"append""append""append""append""append":
-
An already existing output file is opened for writing at the end of the
file in text format.
- 'input_binary'"input_binary""input_binary""input_binary""input_binary""input_binary":
-
An already existing input file is opened for reading in binary format.
- 'output_binary'"output_binary""output_binary""output_binary""output_binary""output_binary":
-
A new output file is opened for writing in binary format.
- 'append_binary'"append_binary""append_binary""append_binary""append_binary""append_binary":
-
An already existing output file is opened for writing at the end of the
file in binary format.
For text files the tuple passed to FileTypeFileTypeFileTypeFileTypefileTypefile_type can be extended by
one of the following encoding settings:
- 'utf8_encoding'"utf8_encoding""utf8_encoding""utf8_encoding""utf8_encoding""utf8_encoding":
-
Strings in the file are encoded in UTF-8.
This is the default, so for UTF-8 encoded files and all files which use
only pure 7-bit US-ASCII characters this value can be omitted.
- 'locale_encoding'"locale_encoding""locale_encoding""locale_encoding""locale_encoding""locale_encoding":
-
Strings in the file are encoded in the local-8-bit encoding which
depends on the system's current locale setting.
Valid encodings are defined, e.g., under Windows by the code pages 1252
(a Microsoft dialect of Latin-1) or 932 (Shift-JIS) or on Linux by the
locales en_US.utf8, de_DE.iso885915, or ja_JP.sjis.
- 'ignore_encoding'"ignore_encoding""ignore_encoding""ignore_encoding""ignore_encoding""ignore_encoding":
-
The encoding of the strings that are read from the file or written into
it is not handled. In that mode multi-byte characters are neither
processed nor interpreted, the operator fread_charfread_charFreadCharFreadCharFreadCharfread_char returns
always one byte, and separator characters that depend on a specific
locale are not handled in fread_linefread_lineFreadLineFreadLineFreadLinefread_line and fread_stringfread_stringFreadStringFreadStringFreadStringfread_string。Furthermore, the strings are not transcoded into or from the current
encoding of the HALCON library.
Note: Strings are still transcoded in the HALCON C/C++ interface,
when the encoding of the HALCON C/C++ interface differs from the encoding
of the HALCON library. For HDevelop scripts, the encoding is always set
to UTF-8. To avoid transcoding of strings to HDevelop, the
encoding of the HALCON library should be set to UTF-8.
For the standard terminal input and output streams, the file names
'standard'"standard""standard""standard""standard""standard" ('input'"input""input""input""input""input" and 'output'"output""output""output""output""output") and
'error'"error""error""error""error""error" (only 'output'"output""output""output""output""output") are reserved.
执行信息
- 多线程类型:可重入(与非独占算子并行运行)。
- 多线程作用域:全局(可从任何线程调用)。
- 未采用并行化处理。
此算子返回一个句柄。请注意,即使该句柄被用作特定算子的输入参数,这些算子仍可能改变此句柄类型的实例状态。
参数
FileNameFileNameFileNameFileNamefileNamefile_name (输入控制) filename → HTuplestrHTupleHtuple (string) (string) (HString) (char*)
Name of file to be opened.
默认值:
'standard'
"standard"
"standard"
"standard"
"standard"
"standard"
建议值:
'standard'"standard""standard""standard""standard""standard", 'error'"error""error""error""error""error", '/tmp/dat.dat'"/tmp/dat.dat""/tmp/dat.dat""/tmp/dat.dat""/tmp/dat.dat""/tmp/dat.dat"
FileTypeFileTypeFileTypeFileTypefileTypefile_type (输入控制) string(-array) → HTupleMaybeSequence[str]HTupleHtuple (string) (string) (HString) (char*)
Type of file access and optional the string encoding.
默认值:
'output'
"output"
"output"
"output"
"output"
"output"
值列表:
'append'"append""append""append""append""append", 'append_binary'"append_binary""append_binary""append_binary""append_binary""append_binary", 'ignore_encoding'"ignore_encoding""ignore_encoding""ignore_encoding""ignore_encoding""ignore_encoding", 'input'"input""input""input""input""input", 'input_binary'"input_binary""input_binary""input_binary""input_binary""input_binary", 'locale_encoding'"locale_encoding""locale_encoding""locale_encoding""locale_encoding""locale_encoding", 'output'"output""output""output""output""output", 'output_binary'"output_binary""output_binary""output_binary""output_binary""output_binary", 'utf8_encoding'"utf8_encoding""utf8_encoding""utf8_encoding""utf8_encoding""utf8_encoding"
FileHandleFileHandleFileHandleFileHandlefileHandlefile_handle (输出控制) file → HFile, HTupleHHandleHTupleHtuple (handle) (IntPtr) (HHandle) (handle)
File handle.
示例(HDevelop)
* Creating an output text file with the name '/tmp/log.txt' and writing
* of a string:
open_file('/tmp/log.txt','output',FileHandle)
fwrite_string(FileHandle,'these are the first and last lines')
fnew_line(FileHandle)
close_file(FileHandle)
结果
如果参数有效,算子
open_fileopen_fileOpenFileOpenFileOpenFileopen_file 返回值 2 ( H_MSG_TRUE )。如有必要,则抛出异常。
可能的后继
fwrite_stringfwrite_stringFwriteStringFwriteStringFwriteStringfwrite_string,
fread_charfread_charFreadCharFreadCharFreadCharfread_char,
fread_stringfread_stringFreadStringFreadStringFreadStringfread_string,
fread_linefread_lineFreadLineFreadLineFreadLinefread_line,
fread_serialized_itemfread_serialized_itemFreadSerializedItemFreadSerializedItemFreadSerializedItemfread_serialized_item,
fwrite_serialized_itemfwrite_serialized_itemFwriteSerializedItemFwriteSerializedItemFwriteSerializedItemfwrite_serialized_item,
fread_bytesfread_bytesFreadBytesFreadBytesFreadBytesfread_bytes,
fwrite_bytesfwrite_bytesFwriteBytesFwriteBytesFwriteBytesfwrite_bytes,
close_fileclose_fileCloseFileCloseFileCloseFileclose_file
另见
close_fileclose_fileCloseFileCloseFileCloseFileclose_file
模块
基础