替换二进制文件中的文本数据 [英] replacing text data in a binary file

查看:91
本文介绍了替换二进制文件中的文本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个快速程序来编辑包含文件路径的二进制文件

(以及其他内容)。如果我查看记事本中的文件,它们看起来像:


< gibberish> file // g:\ pathtofile1< gibberish> file // g:\ pathtofile2< gibberish> ;




我想删除g:\从文件路径。我写了一个控制台应用程序,

成功读取文件并写了一份副本,但是因为某些

的原因而无法执行替换操作。 g:\。代码后跟一个注释

显示不起作用的行。当我看到s时,在
中断模式中的变量,我看到VB没有显示整个文件内容,即使是当我写s时,也不会显示
。到第二个文件流,整个原始文件

是重复的。我想这是因为文件内容并不打算将b $ b解释为字符串(毕竟是它的二进制文件)。它可能会打击

一些不友好的字节,它不能解释为字符串操作,如

替换。如果是这样的话,也许我需要与它一起交换一个角色

一次,虽然我不知道如何以这种方式替换它。任何想法

或代码将不胜感激。你可以告诉我,我做的不多。

二进制文件I / O.


Sub Main()

''将二进制文件读成字符串var

Dim fs As New FileStream(" C:\ Source \Sample and

Demo\Foobar\EditFpl \Only Fools.fpl",FileMode.Open,FileAccess.Read)

Dim sr As New StreamReader(fs,System.Text.Encoding.UTF8)

Dim s As String = sr.ReadToEnd

sr.Close()

fs.Close()


''删除硬 - 编码驱动器盘符规范

s.Replace(" file:// G:\"," file://")''此行不工作


''用我的更改写一个新的二进制文件

Dim fs2 As New FileStream(" C:\ Source \ Sample and

Demo \Foobar \EditFpl \Only Fools2.fpl",FileMode.CreateNew,FileAccess.Write)

Dim sw As New StreamWriter(fs2,System.Text.Encoding.UTF8)

sw.Write(s)


sw.Close()

fs2.Close()

End Sub

I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:

<gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>
etc.

I want to remove the "g:\" from the file paths. I wrote a console app that
successfully reads the file and writes a duplicate of it, but fails for some
reason to do the "replacing" of the "g:\". The code follows with a note
showing the line that is not working. When I look at the "s" variable in
break mode, I see that VB does not show the entire file contents, even
though when I write "s" to the second file stream, the entire original file
is duplicated. I suppose this is because the file content isn''t intended to
be interpreted as a string (its binary after all). It is probably hitting
some unfriendly bytes that it can''t interpret for string operations like
Replace. If that''s the case, maybe I need to interact with it a character at
a time, although I''m not sure how I would do a replace that way. Any ideas
or code would be greatly appreciated. As you can tell, I don''t do much
binary file I/O.

Sub Main()
''read the binary file into a string var
Dim fs As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools.fpl", FileMode.Open, FileAccess.Read)
Dim sr As New StreamReader(fs, System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd
sr.Close()
fs.Close()

''remove the hard-coded drive letter specification
s.Replace("file://G:\", "file://") ''THIS LINE DOES NOT WORK

''write a new binary file with my changes
Dim fs2 As New FileStream("C:\Source\Sample and
Demo\Foobar\EditFpl\Only Fools2.fpl", FileMode.CreateNew, FileAccess.Write)
Dim sw As New StreamWriter(fs2, System.Text.Encoding.UTF8)
sw.Write(s)

sw.Close()
fs2.Close()
End Sub

推荐答案

嗨Adam,


一个简单的因为你不是第一个那样做的人。


s = s.Replace(" file:// G:\"," file://")''此行无效





dim b as String = s.Replace .......


在你问为什么之前


:-)


我希望这有帮助吗?


Cor
Hi Adam,

A simple one because you are not the first one who did that as you do .

s = s.Replace("file://G:\", "file://") ''THIS LINE DOES NOT WORK

or

dim b as String = s.Replace.......

Before you ask why

:-)

I hope this helps?

Cor


* Adam J. Schaff < AB **** @ adelphia.net> scripsit:
* "Adam J. Schaff" <ab****@adelphia.net> scripsit:
''删除硬编码的驱动器号规范
s.Replace(" file:// G:\"," file://") ''这条线不工作
''remove the hard-coded drive letter specification
s.Replace("file://G:\", "file://") ''THIS LINE DOES NOT WORK




''替换''是一个功能,所以它返回结果:


\\\

s = s.Replace(...)

///


- -

Herfried K. Wagner [MVP]

< URL:http://dotnet.mvps.org/>



''Replace'' is a "function", so it returns the result:

\\\
s = s.Replace(...)
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


奥地利山区的回声越来越多。
From the mountains in Austria are comming more and more echo''s.
''Replace''是一个函数,所以它返回结果:

\\\
s = s.Replace(...)
///
''Replace'' is a "function", so it returns the result:

\\\
s = s.Replace(...)
///


这篇关于替换二进制文件中的文本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆