C# 相当于 VB6 的“Open” & “放”功能
我会尽力让这件事尽可能简单。这个问题并不简单地涉及读取和写入字节。我正在寻找此 VB6 代码和 C# 代码之间的精确翻译。我知道这并不总是可行,但我确信有人有一些想法!
VB6代码&解释:
以下代码将数据写入文件的特定部分。
[ Put [#]filenumber, [byte position], varname ].
这是我无法弄清楚的*字节位置* - 非常感谢您提供帮助!
Dim file, stringA as string
Open file for Binary As #1
lPos = 10,000
stringA = "ThisIsMyData"
Put #1, lPos, stringA
Close #1
因此,我再次寻求有关字节位置的帮助。在此示例中,字节位置由 lPos 表示。
编辑 HENK -
我将读取二进制数据。我需要替换此二进制数据中的一些字符。因此,我将使用 VB6 的 instr 函数来获取该数据的位置(长度是先前已知的)。然后,我将使用 Vb6 的 Put 函数将此数据写入新找到的位置。这将用新数据覆盖旧数据。我希望这有帮助!
如果它对任何人有帮助,这里是一些进一步的有关 Put
函数的信息。
非常感谢, 埃文
I will try to make this as straight forward as possible. This question does not simply involve reading and writing bytes. I am looking for an exact translation between this VB6 code and C# code. I know this is not always a posibility but I'm sure someone out there has some ideas!
VB6 Code & explanation:
The below code writes data into a specific part of the file.
[ Put [#]filenumber, [byte position], varname ].
It is the *byte position * that I am having trouble figuring out - and help with this would be very much appreciated!
Dim file, stringA as string
Open file for Binary As #1
lPos = 10,000
stringA = "ThisIsMyData"
Put #1, lPos, stringA
Close #1
So, I am looking for some help with the byte position, once again. In this example the byte position was represented by lPos.
EDIT FOR HENK -
I will be reading binary data. There are some characters in this binary data that I will need to replace. For this reason, I will be using VB6's instr function to get the poisition of this data (there lengths are previously known). I will then use Vb6's Put function to write this data at the newfound position. This will overwrite the old data with the new data. I hope this helped!
If it helps anyone, here is some further information regarding the Put
function.
Thanks so much,
Evan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用 BinaryWriter 吗?
例如:
Can you not use a BinaryWriter?
For example:
您可以使用 StreamReader 和 StreamWriter 来完成此操作。
我会尝试这样的事情:
这个问题并不完美,但它显示了使用文本(不是二进制数据)的类似技术: 将数据插入文本文件
You can do this using StreamReader and StreamWriter.
I would try something like this:
This question is not a perfect fit, however it shows a similiar technique using text (not binary data): Insert data into text file
专门针对此重载查看 StreamWriter 类 Write 方法,它允许您开始写入特定位置内 溪流。
Take a look at the StreamWriter Class specially at this overload of the Write method, which allows you to start writing to a specific place within the stream.