473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Writing a byte to a specific location in a file

I have the need to write a byte of information to a specific location in a
text file.

eg. the file looks something like this.

FYYNN
Line 1
Line 2
<eof>

The first character is a flag indicating the file has changed and the 2 -
4th characters are flags indicating the line of data that has changed ie Y or
N.

What I need to do is set the last four characters on the first line and then
write a line coresponding to each of the 'Y' flags. Finally and it must be
the last thing I do is set the first character on the first line to a flag
which is used by a second application to initate an action.

I hope this makes sense.

initial myfile.txt
WNNNN
<eof>

Below is a sub that I am using to try and update the first flag.

Sub WriteFile()

Dim fs As New System.IO.FileStream("C:\myFile.txt",
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
Dim w As New System.IO.BinaryWriter(fs)

fs.Seek(1, IO.SeekOrigin.Begin)
w.Seek(1, SeekOrigin.Begin)
w.Write(CChar("X"))
End Sub 'Main

Resulting myfile.txt
ïX¿WNNNN
<eof>

Jun 16 '06 #1
2 3014
If you want to write directly to the file you have to first determine
what kind of file it is, then encode the characters according to this.
That is far more complicated than your attempt.
It looks like it's an 8-bit unicode file (UTF8), and that you have
written the character in the middle of the preamble (which is an
encoding identifier at the beginning of the file).

The preamble for an UTF8 file is the three bytes EF BB BF. As you seek
to the second byte in the file (as the index is zero based) you
overwrite the second and third bytes of the preamble. The reason that
you are writing two bytes to the file is that the Char data type in .NET
is a 16 bit unicode character, and writing that using a BinaryWriter
results in two bytes.

After you written to the file, it will contain this data:

EF 'X' 00 'W' 'N' 'N' 'N' 'N'

As this is no longer a unicode file (as you destroyed the preamble), it
will be read as a regular ANSI text file, and the EF and 00 bytes will
show as "weird" characters.
The easiest way to do this is to simply read the entire file, make the
changes, and write it all back again.
Craig wrote:
I have the need to write a byte of information to a specific location in a
text file.

eg. the file looks something like this.

FYYNN
Line 1
Line 2
<eof>

The first character is a flag indicating the file has changed and the 2 -
4th characters are flags indicating the line of data that has changed ie Y or
N.

What I need to do is set the last four characters on the first line and then
write a line coresponding to each of the 'Y' flags. Finally and it must be
the last thing I do is set the first character on the first line to a flag
which is used by a second application to initate an action.

I hope this makes sense.

initial myfile.txt
WNNNN
<eof>

Below is a sub that I am using to try and update the first flag.

Sub WriteFile()

Dim fs As New System.IO.FileStream("C:\myFile.txt",
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
Dim w As New System.IO.BinaryWriter(fs)

fs.Seek(1, IO.SeekOrigin.Begin)
w.Seek(1, SeekOrigin.Begin)
w.Write(CChar("X"))
End Sub 'Main

Resulting myfile.txt
ïX¿WNNNN
<eof>

Jun 16 '06 #2
Thankyou,
I'm still not sure I have done this the best way, but given I have to write
the first byte last it works.. Thankyou Göran.

As it turns out the file I need to write to is plain Ascii, and when I
created this file for testing I created it in UTF8. First Problem!

Once I sorted that, I changed my writing routine to include ascii encoding
and it seems to work as intended.

For interest the new routine was ammended to look like like this,

Sub WriteFile()
Dim fs As New System.IO.FileStream("C:\myfile.txt", _
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, _
FileShare.ReadWrite)
Dim w As New System.IO.BinaryWriter(fs, Encoding.ASCII)
w.Seek(0, SeekOrigin.Begin)
w.Write(CChar("X"))
End Sub
"Göran Andersson" wrote:
If you want to write directly to the file you have to first determine
what kind of file it is, then encode the characters according to this.
That is far more complicated than your attempt.
It looks like it's an 8-bit unicode file (UTF8), and that you have
written the character in the middle of the preamble (which is an
encoding identifier at the beginning of the file).

The preamble for an UTF8 file is the three bytes EF BB BF. As you seek
to the second byte in the file (as the index is zero based) you
overwrite the second and third bytes of the preamble. The reason that
you are writing two bytes to the file is that the Char data type in .NET
is a 16 bit unicode character, and writing that using a BinaryWriter
results in two bytes.

After you written to the file, it will contain this data:

EF 'X' 00 'W' 'N' 'N' 'N' 'N'

As this is no longer a unicode file (as you destroyed the preamble), it
will be read as a regular ANSI text file, and the EF and 00 bytes will
show as "weird" characters.
The easiest way to do this is to simply read the entire file, make the
changes, and write it all back again.
Craig wrote:
I have the need to write a byte of information to a specific location in a
text file.

eg. the file looks something like this.

FYYNN
Line 1
Line 2
<eof>

The first character is a flag indicating the file has changed and the 2 -
4th characters are flags indicating the line of data that has changed ie Y or
N.

What I need to do is set the last four characters on the first line and then
write a line coresponding to each of the 'Y' flags. Finally and it must be
the last thing I do is set the first character on the first line to a flag
which is used by a second application to initate an action.

I hope this makes sense.

initial myfile.txt
WNNNN
<eof>

Below is a sub that I am using to try and update the first flag.

Sub WriteFile()

Dim fs As New System.IO.FileStream("C:\myFile.txt",
System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
Dim w As New System.IO.BinaryWriter(fs)

fs.Seek(1, IO.SeekOrigin.Begin)
w.Seek(1, SeekOrigin.Begin)
w.Write(CChar("X"))
End Sub 'Main

Resulting myfile.txt
ïX¿WNNNN
<eof>

Jun 16 '06 #3

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

Similar topics

10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
4
by: TRW1313 | last post by:
I'm looking to populate a byte array of some fixed size to send out over a UDP connection. The data in the byte array is mixed between characters and binary. I'm a beginner to this language. ...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
0
by: bohuge | last post by:
Hey! At the time being I'm working on a backup solution for a Qtek9090 pocketpc, which should be able to find and backup outlook data to a server, local files, messages and contact from the sim...
3
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.