472,779 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 2972
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.