我试图理解,当有人试图用下一种方式写入文件时,会发生什么:
q)h:hopen `:out
q)h (1 2;3)
3i
q)hclose h
q)read1 `:out
0x07000200000001000000000000000200000000000000f90300000000000000这与二进制表示不同:
q)-8!(1 2;3)
0x010000002d00000000000200000007000200000001000000000000000200000000000000f90300000000000000(1 2;3)写入:out的数据格式-9!-8!(1 2;3)一样set/get如何与这些二进制数据格式相关?-它们是否使用第三种不同的二进制数据格式?发布于 2022-06-03 15:20:01
从技术上讲,如果您对对象有一些先验知识,就可以读取该对象,这样您就可以创建一个标题:
q)read1`:out
0x07000200000001000000000000000200000000000000f90300000000000000
q)-9!read1`:out
'badmsg
[0] -9!read1`:out
^
q)-9!{0x01,0x000000,(reverse 0x0 vs `int$count[x]+1+3+4+1+1+4),0x00,0x00,(reverse 0x0 vs 2i),x}read1`:out
1 2
3这里的标题由以下内容组成:
0x01 - little endian
0x000000 - filler
message length (count of raw `x` plus the header additions)
0x00 - type (generic list = type 0) ... you have to know this in advance
0x00 - attributes .... none here, you would have to know this
length of list .... here we knew it was a 2-item list
x - the raw bytecode of the object without header 正如rianoc所指出的,有更好的方法来编写这样的对象,这样就可以更容易地阅读它们,而不需要先进的知识。
发布于 2022-06-03 13:45:32
-8!返回IPC字节表示。
在磁盘上,二进制格式不同。若要读取此数据,请使用到达。
要将数据流到日志文件,首先需要创建空文件( https://code.kx.com/q/kb/replay-log/#replaying-log-files )
q)`:out set () /This important part adds a header to the file to set the type
`:out
q)h:hopen `:out
q)h (1 2;3)
600i
q)hclose h
q)get `:out
1 2
3注如果您希望将项目作为单个元素写入列表,请使用enlist
q)`:out set ()
`:out
q)h:hopen `:out
q)h enlist (1 2;3)
616i
q)hclose h
q)get `:out
1 2 3二进制和文本数据也可以写入文件,这是您正在做的。
https://code.kx.com/q/ref/hopen/#files
其目的是编写特定的数据片段。
q)h 0x2324 /Write some binary
q)h "some text\n" /Write some text在您的代码中,可以编写原始KX对象的二进制表示,但是没有添加任何头(它既不是IPC格式,也不是磁盘格式)。因此,当您读取数据时,无论是-9!还是get都无法正确地解释这些数据。
使用`:out set ()创建的有效文件二进制文件有一个文件头:(可由get读取)
q)read1 `:out
0xff0100000000000000000200000007000200000001000000000000000200000000000000f90300000000000000带有IPC标头的有效IPC二进制文件:(可读到-9!)
q)-8!(1 2;3)
0x010000002d00000000000200000007000200000001000000000000000200000000000000f90300000000000000二进制文件中的原始对象-不显示头以启用解释。
q)read1 `:out
0x07000200000001000000000000000200000000000000f90300000000000000https://stackoverflow.com/questions/72488931
复制相似问题