Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi ,
I have one binary file , i need to insert one word in this .

i am using below code :
C#
string strFile = File.ReadAllText(outputFile);
    strFile = Regex.Replace(strFile, @"(.)(\*p\dx\dY)", @"$1&l0L$1$2");
  File.WriteAllText(outputFile, strFile);


but after writing the file , actual file content has got change but it should not change anything else except insertion .So what should be the right way to write into binary file ?
Posted
Updated 17-Oct-13 18:09pm
v4
Comments
PIEBALDconsult 18-Oct-13 0:15am    
Maybe it's your Regular Expression? What is it trying to do?
But if it's a "binary file" why are you using it as a "text file"?
lalit.mca2006 18-Oct-13 0:21am    
Yes you are right exactly , Can u please tell me how to do same thing in binary file ?

1 solution

Your code did not change anything by one simple reason: the matching string wasn't found by Regex. If you actually see it in the file but it is not found may indicate that the file is binary. Not all content is a correct string in the encoding you use. Characters does not have one-to-one correspondence with bytes (it depends on encoding). So, you need to use binary approach, even when you replace a string. A binary file may contain a valid string in any encoding, but not every binary file contain a valid string. You can use instead:
http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx[^].

Then, to be able to insert, you have to change the size of output array. So, the intermediate container should be resizeable. It could be System.Collections.Generic.List<byte>, and at the end, before writing, you can use its ToArray() method.

I assumed the file is not very big, as you already had the copy of it in memory. But, if the file is big, you also could use the classes System.IO.BinaryReader and System.IO.BinaryWriter:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^].

Insertion/replacement would require intermediate storage: you should break the file in three parts (before patter, the pattern itself, after pattern) and read them separately (in practice, it's enough to remember the indices of those fragments) and then write in three different parts, with middle part replaces.

You will have to write your own search algorithm though. So, wouldn't it be better to review your date storage format? Think about it. Unfortunately, you did not share your goals, so I cannot advise more on that.

—SA
 
Share this answer
 
Comments
lalit.mca2006 18-Oct-13 0:03am    
Thanks SA for your reply .
my goal is just to insert "&01l" into particular location . This Location i am getting by finding one pattern .before this pattern i need to insert and this pattern must be there in my file and it will be only one time not more than that .
Am i clear about my requirement ?
Sergey Alexandrovich Kryukov 18-Oct-13 0:30am    
No, this is not the goal. What is the goal of setting this goal? :-) Insertions are inefficient and awkward in implementation.
How do you know that your requirements are good enough? I doubt it.

This information is 1) not useful yet; it does not help to advise different data structures; 2) you already got a pretty complete answer on how to do it. You should rethink your bigger picture and think of more suitable data structures. Binary file with a need for regular expression replacement is almost a nonsense. You could get a better advice if you explained why do you need all that.

—SA
lalit.mca2006 18-Oct-13 0:45am    
all i need because i want to add some keyword in printer job language(PJL).Its not human readable format .
When i tried manually to insert specific keyword in this file , i got my expected output .
So that is reason i want to do same thing programatically .
Sergey Alexandrovich Kryukov 18-Oct-13 0:52am    
Okay, thank you for clarification. It would be an important part of the question, as now I see that probably you really could use this way. As I can understand, the language is not quite unreadable, it is said that it's based on "plain English words", but it has compression option and other things (and it all doesn't look good to me).

But then, the approach I already suggested should work for you. You cannot go wrong with binary mode... Is anything unclear?

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900