FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
Go Back  Xtreme Visual Basic Talk > > > FileOpen and FileGet in VB.Net, converting from VB6.


Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2010, 01:24 AM
Disco Stu's Avatar
Disco Stu Disco Stu is offline
Regular
 
Join Date: Jan 2005
Location: Gold Coast, Australia
Posts: 69
Angry FileOpen and FileGet in VB.Net, converting from VB6.


Hi all,

I am trying to convert a VB6 App to VB.Net and I am stuck on this VB6 part...

Open pFileName For Binary As #inIMG
str_TChar = String(sIMG, " ")
Get #inIMG, , str_TChar
ArrIMG(NumberofImages + 1).in_6 = ArrIMG(NumberofImages + 1).in_6 & str_TChar
Close #inIMG

the str_TChar in the VB6 debug window looks like this...
"˙Ĝ˙àJFIF``˙áXExifII*1>QÁQ #.Q#.Adobe ImageReady0˙ÛC 

 $.' ",#(7),01444'9=82

I have converted the code to VB.Net and have this...
inIMG = FreeFile()
FileOpen(inIMG, pFileName, OpenMode.Binary, , , sIMG) 'as #inIMG
str_TChar = " "
String.IsNullOrWhiteSpace(str_TChar)
FileGet(inIMG, str_TChar, sIMG)
ArrIMG(NumberofImages).in_6 = ArrIMG(NumberofImages).in_6 & str_TChar
FileClose(inIMG)

The str_TChar looks like this...
"Ù"

In the VB6 version it looks like the str_TChar is the length of the file in bytes, which is the sIMG variable and the string is in ASCII code?

Can anyone tell me how to get the str_TChar in the VB.Net version to look like the VB6 version please?

Any ideas or help is appreciated.

Thanks and Cheers

From Disco Stu.
Reply With Quote
  #2  
Old 12-20-2010, 10:36 AM
AtmaWeapon's Avatar
AtmaWeaponFileOpen and FileGet in VB.Net, converting from VB6. AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Using FileOpen() isn't converting to VB .NET. It's using compatibility methods meant to tide you over while converting to VB .NET. Here's a good primer on File I/O in VB .NET; I'll admit it's a bit less... accessible than File I/O in some other languages, but it's written for the OO .NET languages and less awkward to use than the VB6 file I/O, which was designed for a structured language.

It's also unwise to perform a conversion by looking for a line-by-line direct translation. Look at it this way. I can say an English phrase like "Farmers make a meager celery" in such a way that you can realize I'm making a wordplay on "salary" and "celery" sounding alike. If I want to translate it to French, I've got a problem. "céleri" is for celery, "salaire" is for salary, and the pronunciation is very different. If I use "céleri", my sentence is exactly the same but the French will think I am an idiot. If I use "salaire" I don't look stupid but the joke is lost. There's *no* correct way to translate that sentence to French unless you include a footnote that explains the wordplay. Another good example: Shakespeare wrote his plays in English. I speak English. I still need extensive footnotes to fully understand and appreciate Shakespeare's play because English has changed significantly since his lifetime and I'm not familiar with current events in the 16th century.

Now to apply that analogy. When you try to convert program code directly, you can make the same kind of mistakes without realizing it. Sometimes a method works differently in the new language and you don't realize it. Sometimes there's better ways to do it. Sometimes it looks like you're speaking moon language to the people who use the new language because you're using 10-year-old methods that have been obsoleted, and in computer terms you may as well be speaking Old English. To port a program, you have to understand it in language-agnostic terms. Only then can you implement the functionality properly in the new language.

I'm not familiar with VB6, but let's make a low-level analysis and turn it into a paragraph. It sounds like you don't quite know what the original code does; it's impossible to convert code you don't understand.
Code:
* Open some file for binary input, use #inIMG as the identifier.
* Create a string that is sIMG in length that contains the 
      space character. (If sIMG is 4, the string is "    ")
* Store the string in str_TChar.
* Read bytes from #inIMG into str_TChar until the string is full?
    (I can't find documentation for Get so I have to guess.)
* Append str_TChar to the end of some value in an array.
* Close #inIMG
It looks like this code reads some number of bytes from a file and appends those bytes to some string. I'm not sure what sIMG is so I can't deduce whether it's reading the entire file or some specific number of items. I also don't know what ArrIMG is or how NumberOfImages is related, so I'm not much help there. Let's look at what your VB .NET attempt does to get an idea of why it is different:
Code:
* Open some file for binary input, using inIMG as the identifier
    and a record length of sIMG.
* Initialize str_TChar to a single space.
* Ask if str_TChar is null or whitespace. (Always true.) Don't
    do anything with the result.
* This one's difficult. The signature is FileGet(Integer, String, Integer).
    From the documentation, that's going to be the file identifier, (inIMG),
    data destination, and offset. So it reads sIMG bytes from the
    file starting at position sIMG, converts the resulting bytes
    to a string, and stores that string in str_TChar.
* Append str_TChar to some element in an array.
* Close inIMG.
This code does something entirely different. Rather than reading the entire file into a string variable, it reads sIMG bytes starting at offset sIMG. Also, it's performing a *conversion* from bytes to string and using whatever encoding .NET feels like using and possibly losing data. The result looks different because you're not reading the same thing at all!

Here's an attempt at mimicing the behavior of the VB6. Some structural changes will be needed. I suggest not using String at all; it's not a random container of bytes. If you want a container of bytes, you should use a Byte array. That means ArrIMG's contents need to change; I don't know what the devil it is in the first place so I can't help with that. I don't like your variable names, either. "IMG" could just as easily mean "International Machine Group" as "Imagine" or "Image". Write what you mean, don't leave puzzles for other programmers or your future self.
Code:
Dim fileContents() As Byte = Nothing
Using fs As New IO.FileStream(fileName, IO.FileMode.Open)
    ' You're on your own if the file's > 2 GB; there's technical reasons I didn't
    ' fool with it.
    Dim fileLength As Integer = CInt(fs.Length)
    ReDim fileContents(fileLength - 1)
    fs.Read(fileContents, 0, fileLength)
End Using

' Here's where you append fileContents to the data in the array; I don't know
' what that's going to look like because I don't have enough information.
You could actually simplify it further with some knowledge of the types in System.IO:
Code:
Dim fileContents() As Byte = IO.File.ReadAllBytes(fileName)

' Here's where you append fileContents to the data in the array; I don't know
' what that's going to look like because I don't have enough information.
If you really want it stowed in a string, you're going to have to provide a reason other than "the original code used a String". There's probably some reason why the original code used a String, and I believe that reason might be "VB6's support for concatenating arrays was nonexistent and ASCII strings had the & operator."
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 12-20-2010, 05:17 PM
passel's Avatar
passelFileOpen and FileGet in VB.Net, converting from VB6. passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

For not knowing VB6, you got most everything right.
The "Get" does read the number of bytes from a binary file necessary to fill the type.
In VB6, an Integer was two bytes, so a Get #fileID, ,intTest would read two bytes from the file at the current offset into the Integer, likewise a Long (4 bytes), would "get" four bytes from the file.
A "Get #fileID, 20, intTest" would read the 2 bytes starting at the 20th byte in the file (first byte being offset 1, not 0).

So, if you create a string 20 characters long, then 20 bytes (interpreted as ASCII) would be "gotten" from the file to fill the string. Interestingly enough, VB6 strings being Unicode, the number of bytes in the string itself was actually 40, but since the Microsoft BASIC binary file I/O existed long before Visual Basic and Unicode characters, this was an exception to the rule of reading the number of bytes necessary to fill the variable.
All string "Put" to the file were converted from Unicode to ASCII when put, and converted from ASCII to Unicode when "Get"(sic).

The small part that you mis-interpreted was:
"...So it reads sIMG bytes from the
file starting at position sIMG, converts the resulting bytes
to a string, and stores that string in str_TChar...."

The record length of sIMG has no meaning on files Opened for Binary (record length field is ignored).
So, in the case in .net where the str_Tchar was preset to 1 character in length, only 1 bytes was read from the file, converted to Unicode, so str_Tchar was still 1 character in length after the read. But you were right that that ASCII character was read from the "sIMG"th byte of the file.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 12-20-2010 at 05:34 PM.
Reply With Quote
  #4  
Old 12-22-2010, 03:17 AM
Disco Stu's Avatar
Disco Stu Disco Stu is offline
Regular
 
Join Date: Jan 2005
Location: Gold Coast, Australia
Posts: 69
Default FileOpen and fileGet

Thanks for the input and the code.

The code you supplied works well....
Dim fileContents() As Byte = Nothing
Using fs As New IO.FileStream(fileName, IO.FileMode.Open)
' You're on your own if the file's > 2 GB; there's technical reasons I didn't
' fool with it.
Dim fileLength As Integer = CInt(fs.Length)
ReDim fileContents(fileLength - 1)
fs.Read(fileContents, 0, fileLength)
End Using

I am yet to see the results as i have now encountered another error and I am still working on it.

Thank again, much appreciated.

Cheers from Disco Stu.
Reply With Quote
  #5  
Old 01-10-2011, 07:22 AM
Disco Stu's Avatar
Disco Stu Disco Stu is offline
Regular
 
Join Date: Jan 2005
Location: Gold Coast, Australia
Posts: 69
Smile Found something else that does the job

Hello again,

After trying the above code, i still needed to convert the string to ASCII code and came across this bit of code, which did the job...

'Need to convert TmpImg to a ASCII String
Dim myFileStream As New FileStream(pFileName, FileMode.Open, _ FileAccess.Read)
Dim bteRead() As Byte
Dim strRead As String
Dim lngRead As Long

lngRead = myFileStream.Length
ReDim bteRead(lngRead)
myFileStream.Read(bteRead, 0, lngRead)
strRead = Encoding.ASCII.GetString(bteRead)
myFileStream.Close()
ArrIMG(NumberofImages).in_6 = strRead

Thanks again for the help.

Cheers.

Disco Stu.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6. FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
 
FileOpen and FileGet in VB.Net, converting from VB6.
FileOpen and FileGet in VB.Net, converting from VB6.
 
-->