Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

convert nulls to spaces in file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Henry - 21 Oct 2004 19:37 GMT
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!
Jay B. Harlow [MVP - Outlook] - 21 Oct 2004 20:05 GMT
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

> first question... I have a flat file which unfortinuatly has columns
> seperated by nulls instead of spaces (a higher up company created it this
[quoted text clipped - 6 lines]
> it back then open it as a stream reader to read it line by line? the lines
> are delimited by CR+LF's thanks!
Mythran - 21 Oct 2004 21:22 GMT
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

> Brian,
> I would simply open it with a System.IO.FileStream, read a buffer full of
[quoted text clipped - 38 lines]
>> it back then open it as a stream reader to read it line by line? the
>> lines are delimited by CR+LF's thanks!
Jay B. Harlow [MVP - Outlook] - 21 Oct 2004 21:35 GMT
Mythran,
Brian (the original poster) stated:
>>> because it is right now causes truncated data at wierd places...

Which leads me to believe something strange is going on with the encoding
when you attempt to read the file into a string (an encoding is required).
There may not be, or it may be something simple, why risk it, when I know
the FileStream won't cause problems, especially on the sample file he
provided...

Reading it as bytes with FileStream will not involve any Encoding objects. I
am reading pure bytes, changing pure bytes, writing pure bytes. Hence I
won't be taking time to track down any potential encoding problems implied
with the Brian's statement.

Hope this helps
Jay

> 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
[quoted text clipped - 44 lines]
>>> it back then open it as a stream reader to read it line by line? the
>>> lines are delimited by CR+LF's thanks!
Mythran - 21 Oct 2004 21:45 GMT
Yes, your way would work...maybe even better...

What I was stating is that he reads it all into a string instead of line by
line (regardless of how he reads it in....such as FileStream or whatever).
Then he just splits the results based on the Null...but my way may not work
anyways because the Split method may not accept DBNull.Value....but it may
take Chr(x).  Hrm, probably would just do it your way if it was me :P

Mythran

> Mythran,
> Brian (the original poster) stated:
[quoted text clipped - 62 lines]
>>>> 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!
Jay B. Harlow [MVP - Outlook] - 21 Oct 2004 22:01 GMT
Mythran,
Brian is referring to a Null Char (ChrW(0)).

If you look at the file he attached to one of his messages the file is fixed
length records. The first couple of records have 20 or 30 zero bytes at the
start of the line.

The ChrW(0) are not being used for record or field delimiters per se.

I would use the Split method if the file was using ChrW(0) for record or
field delimiters.

> What I was stating is that he reads it all into a string instead of line
> by line (regardless of how he reads it in....such as FileStream or
> whatever).
FileStream cannot read a file into a string, it only reads Bytes. To read a
file into an System.Text.Encoding object is needed, as System.Text.Encoding
is used to convert the bytes in the file to Unicode which is what Strings
are. StreamReader uses an Encoding object to convert the bytes to a String,
if you use a FileStream you would need to create your own Encoding object &
use it to convert the bytes into a String. As I stated Brian's original
statement suggested that the Encoding object may have been having problems
with the 20 or 30 zero bytes...

Hope this helps
Jay

> Yes, your way would work...maybe even better...
>
[quoted text clipped - 73 lines]
>>>>> 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!
Mythran - 21 Oct 2004 22:42 GMT
Ahh, didn't look at the file...but according to his first post, the columns
are separated by Null.  Which I read as "ROWS" separated by NULL...so
scratch everything I've said in this thread...

Sometimes you just have to NOT listen to me at all :P

Mythran

> Mythran,
> Brian is referring to a Null Char (ChrW(0)).
[quoted text clipped - 101 lines]
>>>>>> stream reader to read it line by line? the lines are delimited by
>>>>>> CR+LF's thanks!
Jay B. Harlow [MVP - Outlook] - 21 Oct 2004 22:57 GMT
Mythran,
> Sometimes you just have to NOT listen to me at all :P
I think that can be said about everybody, me included :-))

Especially now that Chris just reminded me that VS.NET itself has trouble
showing strings with ChrW(0) in them, which suggests that Brian's Encoding
"problem" I suspect is more related to the debugger then to the Encoding
object I was suspecting...

Jay

> Ahh, didn't look at the file...but according to his first post, the
> columns are separated by Null.  Which I read as "ROWS" separated by
[quoted text clipped - 3 lines]
>
> Mythran

<<snip

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.