473,624 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.FileS tream("C:\myFil e.txt",
System.IO.FileM ode.Open, System.IO.FileA ccess.ReadWrite )
Dim w As New System.IO.Binar yWriter(fs)

fs.Seek(1, IO.SeekOrigin.B egin)
w.Seek(1, SeekOrigin.Begi n)
w.Write(CChar(" X"))
End Sub 'Main

Resulting myfile.txt
ïX¿WNNNN
<eof>

Jun 16 '06 #1
2 3033
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.FileS tream("C:\myFil e.txt",
System.IO.FileM ode.Open, System.IO.FileA ccess.ReadWrite )
Dim w As New System.IO.Binar yWriter(fs)

fs.Seek(1, IO.SeekOrigin.B egin)
w.Seek(1, SeekOrigin.Begi n)
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.FileS tream("C:\myfil e.txt", _
System.IO.FileM ode.Open, System.IO.FileA ccess.ReadWrite , _
FileShare.ReadW rite)
Dim w As New System.IO.Binar yWriter(fs, Encoding.ASCII)
w.Seek(0, SeekOrigin.Begi n)
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.FileS tream("C:\myFil e.txt",
System.IO.FileM ode.Open, System.IO.FileA ccess.ReadWrite )
Dim w As New System.IO.Binar yWriter(fs)

fs.Seek(1, IO.SeekOrigin.B egin)
w.Seek(1, SeekOrigin.Begi n)
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
3277
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 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...
289
1805
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 stay with C and still be able to produce Java byte code for platform independent apps. Also, old programs (with some tweaking) could be re-compiled and ported to the JVM. We have been developing such a tool over the last 2 years and currently...
4
10406
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. I can assign the character data by the byte. However, I'm not sure how I would assign a binary value to a certain location in a byte array where the value is greater than 255. I have an unsigned short and want to assign it to a
2
6850
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
0
2252
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 card (and backup them to a server as well) all written in C#. So I'm making a wrapper for the sim manager api. In order to use it's functions in my device application, I've used platform invoke method, to acces the simmanager functions from...
3
3419
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 height;
6
2169
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 and/or processes so that multiple threads/processes can write to the same file (all threads use the same instance of the stream, processes use a different instance but still may point to the same file) Could you point me in the correct...
10
6362
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 and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
2
2161
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 streamwriter, it looses the final packet. Strangely, If I output the datapackets to a console from the telnet server, it perfectly gets all the output packets. the code snippet is below: StreamWriter sw = new StreamWriter("c:\\output.txt",...
0
8242
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8488
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.