且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将空值转换为文件中的空格

更新时间:2023-02-23 09:38:00

Brian,

我只需用System.IO.FileStream打开它,读取一个充满

字节的缓冲区,将空字节更改为AscW(" c)然后将缓冲区写入秒

FileStream。


类似于:


进口System.IO


Dim input As New FileStream(" input.txt",FileMode.Open)

Dim output As New FileStream(" output.txt",FileMode.Create)


Dim buffer(1023)By Byte

Dim length As Integer

Do

length = input.Read(buffer ,0,buffer.Length)

索引As Integer = 0 To length - 1

如果buffer(index)= 0则

buffer (index)= ascW(" c)

结束如果

下一页

output.Write(buffer,0,length)

循环直到长度< buffer.Length

input.Close()

output.Close()


注意以上内容可能无法正常工作Ansi 8位编码,但是它应该很容易适应。


希望这有帮助

Jay >
" Brian Henry" &LT峰; br ********** @ newsgroups.nospam&GT;在消息中写道

新闻:eD ************* @ TK2MSFTNGP15.phx.gbl ...
Brian,
I would simply open it with a System.IO.FileStream, read a buffer full of
bytes, change the null bytes to AscW(" "c) then write the buffer to a second
FileStream.

Something like:

Imports System.IO

Dim input As New FileStream("input.txt", FileMode.Open)
Dim output As New FileStream("output.txt", FileMode.Create)

Dim buffer(1023) As Byte
Dim length As Integer
Do
length = input.Read(buffer, 0, buffer.Length)
For index As Integer = 0 To length - 1
If buffer(index) = 0 Then
buffer(index) = AscW(" "c)
End If
Next
output.Write(buffer, 0, length)
Loop Until length < buffer.Length
input.Close()
output.Close()

Note the above may not work correctly for non Ansi 8-bit encodings, however
it should be easy enough to adapt.

Hope this helps
Jay
"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:eD*************@TK2MSFTNGP15.phx.gbl...
第一个问题......我有一个平面的文件,其中不幸的是列有空格而不是空格(一个更高的公司创建它对我们这个方式)无论如何要用这个做一个readline而没有它
受null影响?因为它现在是在奇怪的地方导致截断的数据...但是一旦我用十六进制编辑器手动将char(00)更改为char(20),它在文件中更好地读取...这引出了我的第二个问题...如果你不能按照我在第一个问题中所说的那样做,那么就有办法浏览文件,将空值转换为空格并保存
由CR + LF分隔!谢谢!
first question... I have a flat file which unfortinuatly has columns
seperated by nulls instead of spaces (a higher up company created it this
way for us) is there anyway to do a readline with this and not have it
affected by the null? because it is right now causes truncated data at
wierd places... but as soon as i manually with a hex editor change
char(00) to char(20) in the files it reads prerfectly... which leads me to
my 2nd question... if you cant do what i said in the 1st question, is
there a way to go through the file, convert the nulls to spaces and save
it back then open it as a stream reader to read it line by line? the lines
are delimited by CR+LF''s thanks!



忘了说测试数据在附加的文本文件中/>

" Brian Henry" &LT峰; br ********** @ newsgroups.nospam&GT;在消息中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...
forgot to say the test data is in the text file attached

"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
这里是一些测试数据在哪里工作......它没有意义的数据,但它显示了重点...这里是我用来读它的代码
它不应该做的是它而不是读这个全线..

456456456456456456 100 156454541
02/20 / 1955FDFHGEL R OCONFGG MGK
K40 09/04/2004 00 Y48 / 17/1982

>它只会读取这个
456456456456456456 100
然后完全停止该行并移动到下一行并执行相同的操作!第一行甚至不会读取任何数据(没有初始文本只有空值,直到000)...一旦ai用空格替换那些ASCII
Char(00)[null]'s ASCII字符(20)它完美运行!...任何方法来解决这个问题?谢谢

如果IO.File.Exists(Me.txtFileName.Text.Trim)那么

Me.lblStatus.Text ="文件存在...试图打开。 ..

''我们有一个文件,现在尝试打开它

Dim fs As New IO.FileStream(Me.txtFileName.Text,IO.FileMode。打开,
IO.FileAccess.Read)

Dim sr As New IO.StreamReader(fs,True)


Me.lblStatus.Text = 文件打开...

直到sr.Peek = -1

debug.writeline(sr.ReadLine)

循环

sr.Close()


结束如果
here is some test data where this works... its meaningless data but it
shows the point... and here is the code i used to read it
What it does that it shouldnt do is instead of reading this whole line..

456456456456456456 100 156454541
02/20/1955FDFHGEL R OCONFGG MGK
K40 09/04/2004 00 Y48/17/1982

it will read only this
456456456456456456 100
then stop completely for that line and move to the next line and do the
same thing! the first line wont even read any data (has no inital text
only null values until the 000)... as soon as a i replace those ASCII
Char(00) [null]''s with spaces ASCII Char(20) it works perfectly!... any
ways to get around this? thanks
If IO.File.Exists(Me.txtFileName.Text.Trim) Then

Me.lblStatus.Text = "File exists... Trying to open..."

'' we have a file, try to open it now

Dim fs As New IO.FileStream(Me.txtFileName.Text, IO.FileMode.Open,
IO.FileAccess.Read)

Dim sr As New IO.StreamReader(fs, True)

Me.lblStatus.Text = "File opened..."

Do Until sr.Peek = -1

debug.writeline(sr.ReadLine)

Loop

sr.Close()

End If



嗯,如何在文件中读取字符串并使用null

char作为分隔符进行拆分?我没有尝试过,也不知道它是否能够工作......但是到底是什么,值得一试呃?


Mythran


" Jay B. Harlow [MVP - Outlook]" &LT; JA ************ @ msn.com&GT;在消息中写道

news:O
Hrm, how about read in the file to a string and do a split with the null
char being the delimiter? I haven''t tried it and don''t know if it will
work...but what the heck, worth a try eh?

Mythran

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O