473,574 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Discrepancies between FileGet and StreamReader

Hello:

When I use either one to read a Text file, I get the same result. The length
of the string that the file's content has been written into is the same.

However, if the file is binary, FileGet gets me the correct content while
StreamReader gives me a truncated string.

Can somebody advise me why? Should I be using BinaryReader instead of
StreamReader? Would BinaryReader work on text files?

Venkat
Nov 21 '05 #1
16 2075
I tried using BinayReader but unlike the simplicity of the one single
statement as follows:

FileGet(ciFileH andle, sFileContent)

which will read the contents of the file into sFileContent, I have to read
one byte at a time and concatenate to sFileContent.

Is there one Stream/Binary class that will work both on text and binary files?
Nov 21 '05 #2
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Can somebody advise me why? Should I be using
BinaryReader instead of StreamReader?
Depends on what you want to do. Notice that you can specify an encoding in
the 'StreamReader'' s constructor.
Would BinaryReader work on text files?


Yes.

\\\
Dim br As New System.IO.Binar yReader( _
New System.IO.FileS tream("C:\foo.t xt", IO.FileMode.Ope n) _
)
Dim abyt() As Byte = br.ReadBytes(br .BaseStream.Len gth)
br.Close()
Dim s As String = System.Text.Enc oding.Default.G etString(abyt)
///

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

Nov 21 '05 #3
Herfried:

Thank you so much for your tip. The BinaryReader, in my view, is more
versatile in that it handles different encoding.

Venkat

"Herfried K. Wagner [MVP]" wrote:
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Can somebody advise me why? Should I be using
BinaryReader instead of StreamReader?


Depends on what you want to do. Notice that you can specify an encoding in
the 'StreamReader'' s constructor.
Would BinaryReader work on text files?


Yes.

\\\
Dim br As New System.IO.Binar yReader( _
New System.IO.FileS tream("C:\foo.t xt", IO.FileMode.Ope n) _
)
Dim abyt() As Byte = br.ReadBytes(br .BaseStream.Len gth)
br.Close()
Dim s As String = System.Text.Enc oding.Default.G etString(abyt)
///

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

Nov 21 '05 #4
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Thank you so much for your tip. The BinaryReader, in my view,
is more versatile in that it handles different encoding.


The streamreader can handle different encodings too, and thus is more
high-level when reading text files.

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

Nov 21 '05 #5
Herfried:

Thanks to you, I am able to read a binary file and store its contents into a
string:

Dim lfsTemp = New FileStream("Ori ginal.INI", IO.FileMode.Ope n)
Dim lbrTemp = New BinaryReader(lf sTemp)
Dim lbTemp = lbrTemp.ReadByt es(lbrTemp.Base Stream.Length)
Dim sFileContent as String =
System.Text.Enc oding.Default.G etString(lbTemp )

I tried saving sFileContent to a new file:

Dim lfsTemp As New FileStream("Cop y.Ini", FileMode.Create New)
Dim lbwTemp As New BinaryWriter(lf sTemp)

SaveFile = False

lbwTemp.Write(s FileContent)
lbwTemp.Close()
lfsTemp.Close()
I see that the two file sizes are not the same.

Can you tell me why?

Venkat

Nov 21 '05 #6
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Thanks to you, I am able to read a binary file and
store its contents into a
string:

Dim lfsTemp = New FileStream("Ori ginal.INI", IO.FileMode.Ope n)
Dim lbrTemp = New BinaryReader(lf sTemp)
Dim lbTemp = lbrTemp.ReadByt es(lbrTemp.Base Stream.Length)
Dim sFileContent as String =
System.Text.Enc oding.Default.G etString(lbTemp )

I tried saving sFileContent to a new file:

Dim lfsTemp As New FileStream("Cop y.Ini", FileMode.Create New)
Dim lbwTemp As New BinaryWriter(lf sTemp)

SaveFile = False

lbwTemp.Write(s FileContent)
lbwTemp.Close()
lfsTemp.Close()
I see that the two file sizes are not the same.

Can you tell me why?


I assume that different encodings are used. If the source file is encoded
in your default code page, then you need 'Encoding.Defau lt' to read it.
Notice that the binary reader does not know that it should use
'Encoding.Defau lt' too to decompose the string into a byte array.
'BinaryWriter' provides a constructor that accepts an encoding:

\\\
Dim br As New BinaryWriter(.. ., Encoding.Defaul t)
....
///

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

Nov 21 '05 #7
John:

As per your advice, I modified my write statement:

Dim lbwTemp As New BinaryWriter(lf sTemp, System.Text.Enc oding.Default)

But I have the following differences:

1. The Original file content is 1205 bytes; the copy file contents is 1207
bytes.
2. I checked the ASC of first character; in Orginal it's 146 and in the
Copy, it's 181.

I see that when reading the file, I had used the following statement to
convert byte to string:

Dim sFileContent as String =
System.Text.Enc oding.Default.G etString(lbTemp )

Should I be converting the string to byte before I write it to the file? If
so, can you help me with the syntax?

Thanks for your continued help.

Venkat

"Herfried K. Wagner [MVP]" wrote:
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Thanks to you, I am able to read a binary file and
store its contents into a
string:

Dim lfsTemp = New FileStream("Ori ginal.INI", IO.FileMode.Ope n)
Dim lbrTemp = New BinaryReader(lf sTemp)
Dim lbTemp = lbrTemp.ReadByt es(lbrTemp.Base Stream.Length)
Dim sFileContent as String =
System.Text.Enc oding.Default.G etString(lbTemp )

I tried saving sFileContent to a new file:

Dim lfsTemp As New FileStream("Cop y.Ini", FileMode.Create New)
Dim lbwTemp As New BinaryWriter(lf sTemp)

SaveFile = False

lbwTemp.Write(s FileContent)
lbwTemp.Close()
lfsTemp.Close()
I see that the two file sizes are not the same.

Can you tell me why?


I assume that different encodings are used. If the source file is encoded
in your default code page, then you need 'Encoding.Defau lt' to read it.
Notice that the binary reader does not know that it should use
'Encoding.Defau lt' too to decompose the string into a byte array.
'BinaryWriter' provides a constructor that accepts an encoding:

\\\
Dim br As New BinaryWriter(.. ., Encoding.Defaul t)
....
///

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

Nov 21 '05 #8
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Should I be converting the string to byte before I write it to the file?
If
so, can you help me with the syntax?

You can try to do that:

\\\
Dim abyt() As Byte = _
System.Text.Enc oding.Default.G etBytes(Content )
....
///

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

Nov 21 '05 #9
Herbert:

I viewed the two files. The copy has two extra bytes added to the front.
That accounts for the difference in size. Let me try converting the string to
a byte and then saving it.

Thanks again.

venkat

"Herfried K. Wagner [MVP]" wrote:
"vvenk" <vv***@discussi ons.microsoft.c om> schrieb:
Should I be converting the string to byte before I write it to the file?
If
so, can you help me with the syntax?

You can try to do that:

\\\
Dim abyt() As Byte = _
System.Text.Enc oding.Default.G etBytes(Content )
....
///

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

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12998
by: Keith Kingsley | last post by:
I'm using a StreamReader to read in several lines from an ASCII file. I'd like to know the StreamReader's "true" position-- that is, the number of bytes into the file that the StreamReader has read. I thought about using MyStreamReader.BaseStream.Position, but this always seems to return a multiple of the StreamReader's buffer size (which...
0
1789
by: TrongAn Hoang | last post by:
Hi, I am converting a VB6 program. I have data from UDT that contained all kinds of VB6 stuff like: variable length strings variants primitive types arrays of UDTs In short, it has all different kinds. I save these to disk using the Put
8
4649
by: M. Angelo | last post by:
Is there a way to speed up FileGet and FilePut? Or should I use another method?
5
1855
by: vvenk | last post by:
Hello: I am trying to read the contents of a file that be either be binary or text. I would like to know which is better, Filestream or FileGet? Thanks. Venkat
0
1465
by: Mike Cooper | last post by:
Hi everyone, I am accessing several binary (PCL) files sequentially using a for loop. For each file I am using the fileget() command to populate the contents of the file into a string. I use the following commands to do so: LengthofOverlay = FileLen(PCLOven_Burn_Directory & OverlayForm) OverlayContent = StrDup(LengthofOverlay, "x")...
2
1328
by: Jack Russell | last post by:
I have some code I converted from VB6 to read some binary files. FileGet of an int16 seems to be returning a zero instead of the correct value. Anybody heard of this, know of a work around? Thanks
3
3103
by: Arpan | last post by:
A file can be read using only the StreamReader object like this: Dim sReader As StreamReader sReader = New StreamReader(Server.MapPath("File1.txt")) While(sReader.Peek -1) Response.Write(sReader.ReadLine) End While as well as using the FileStream object along with the StreamReader object like this:
1
2721
by: Sladan | last post by:
Im trying to read a xml-file with a StreamReader. For the moment I'm using the following code. streamReader = new StreamReader(stream, System.Text.Encoding.Default); string feedData = streamReader.ReadToEnd(); I'm using System.Text.Encoding.Default so that I can get some swedish characters working. But I'm having problem when reading a...
0
2340
by: rajana | last post by:
Dear All, We have Ansi file with german characters (Ä / Ø) , We are using Streamreader to read the contents of the file. But Readline() not able to read the German characters. We tried all possibilities of calling the streamreader, but nothing worked. Dim sr As StreamReader = New StreamReader(Filename, System.Text.Encoding.Default,...
0
7735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7823
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8102
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6458
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2250
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.