Problem reading Chr(0) from random file...

G

Guest

hai,
At present I'm facing a strange problem. We are converting a project from
VB to .NET
One record contains Chr(0). Let the record be of length 1800. In the 50th
byte of a record a chr(0) is there.
While I use VB to read it's giving all 1800 bytes. But when I converted my
VB to VB.NET, after the 49th byte all the bytes are truncating. I have to
solve this issue to proceed further. I hope the experts around here can solve
my problem.
I'm using the random file and using a structure of length 1800 bytes.
Following is the code I use

Public Structure StudentRCDetails
<VBFixedString(1800)> Dim lsData As String
End Structure

Dim lStructDetails As StudentRCDetails

FileOpen(liKeyFileNum, lsFilePath, OpenMode.Binary, , OpenShare.Shared, 1800)
FileGet(liKeyFileNum, lStructDetails , Integer.Parse(lsStudentID))
FileClose(liKeyFileNum)


I'm getting only a part of a string. ie. till the Chr(0). After, all the
datas are discarded.
I tried with opening in Random mode also. The result is same for both the
cases.
Please help me in this issue. It's urgent

regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venkatarajan said:
At present I'm facing a strange problem. We are converting a project from
VB to .NET
One record contains Chr(0). Let the record be of length 1800. In the 50th
byte of a record a chr(0) is there.
While I use VB to read it's giving all 1800 bytes. But when I converted my
VB to VB.NET, after the 49th byte all the bytes are truncating. I have to
solve this issue to proceed further. I hope the experts around here can solve
my problem.

Are you *absolutely sure* that it's actually being truncated? How are
you examining the string? If it's in the debugger, try printing out the
string - you may well find that the rest of the data is there, it's
just not being shown in the debugger.

(I would be more confident about this if you were using standard .NET
IO classes - I'm not familiar with the VB functions you're using.)
 
G

Guest

Hai Jon,
I tried to print it out in the debugger?. Let assume the string be
"Venkat" & Chr(0) & "rajan".
When I tried to print it in the debugger it shows as
"Venkat
When try to extract character by character, it shows me a error.
Not even the terminating quotes.

Help me out

Regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venki said:
I tried to print it out in the debugger?. Let assume the string be
"Venkat" & Chr(0) & "rajan".
When I tried to print it in the debugger it shows as
"Venkat
When try to extract character by character, it shows me a error.
Not even the terminating quotes.

Help me out

Don't print it out in the debugger - print it out in a console window.
Basically, don't trust the debugger when it comes to strings, until you
know exactly how it handles them.
 
G

Guest

I tried it in Command Window. It gives the same result. Help me to solve this
issue.

Regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venki said:
I tried it in Command Window. It gives the same result. Help me to solve this
issue.

Don't try it in the command window, try it in the console - using
Console.WriteLine - as I suggested before.
 
G

Guest

hai jon,
It's working fine when I tried with Console Application. But when did the
same with Web Services or Windows application the string is getting
truncated. Please help me in this issue.
Dim str1 As String
str1 = "Venkat" & Chr(0) & "Rajan"
Console.WriteLine(str1)
Console.ReadLine()
It gave Venkat Rajan in Console Application.

Dim str1 As String
str1 = "Venkat" & Chr(0) & "Rajan"
msgbox(str1)
When I used a windows Application , it showed me only Venkat.
Thanks in Advance.

Regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venki said:
It's working fine when I tried with Console Application. But when did the
same with Web Services or Windows application the string is getting
truncated. Please help me in this issue.
Dim str1 As String
str1 = "Venkat" & Chr(0) & "Rajan"
Console.WriteLine(str1)
Console.ReadLine()
It gave Venkat Rajan in Console Application.

Dim str1 As String
str1 = "Venkat" & Chr(0) & "Rajan"
msgbox(str1)
When I used a windows Application , it showed me only Venkat.
Thanks in Advance.

That's just because message box will truncate strings. The string
itself is fine.

If you want to get rid of the nul characters, use String.Replace and
replace them with whatever you want (eg space).
 
G

Guest

Hai Jon,
See my first post. I'm getting the Structure of length 1800 bytes. When I
tried to read it from the file, the character what I'm getting is terminated
when it reaches the null string. I have solve this issue.
Thanks in advance

Regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venki said:
See my first post. I'm getting the Structure of length 1800 bytes.
When I tried to read it from the file, the character what I'm getting
is terminated when it reaches the null string. I have solve this
issue.

I'm still not convinced it *is* actually being terminated - have you
tried printing out the string *to the console* (not the debugger, not
the command window, not a web page, not a message box) after reading
it?
 
G

Guest

Hai Jon,
As you said I tried to print it in the console. It's displaying
correctly. But when tried to do some calculations based on the string, I'm
unable to get the whole part of the string.
Thanks in advance
regards,
Venkatarajan
 
J

Jon Skeet [C# MVP]

Venki said:
As you said I tried to print it in the console. It's displaying
correctly. But when tried to do some calculations based on the string, I'm
unable to get the whole part of the string.

You should be able to, just using the standard .NET string access
methods. What's failing for you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top