1

I'm making a program in delphi that writes data to a binary file and I have a problem I do not understand how to solve.

I have the following code:

testar: = TFileStream.Create ('test.exe', fmOpenWrite);
testar.Position: = testar.Size;

here: = '{test} test {test}';

testar.Write (here, 1024);

Tested with WinHex

http://img836.imageshack.us/img836/3206/la49.jpg

This edition fine prints in the binary code because when I see it with WinHex looks good, but this other code:

testar: = TFileStream.Create ('test.exe', fmOpenWrite);
testar.Position: = testar.Size;

here: = '{test}' + Edit1.Text + '{test}';

testar.Write (here, 1024);

It does not show anything at all because it seems that there is a problem with the edit when you want to edit the binary code, it's weird because when I use it all goes single quotes but with the example of the edit does not work.

Note: The program does not give any error message

Someone could help me with this problem ?

3
  • Where and how is testar defined? Also you have lots of syntax errors in the code you are showing which I assume are copy errors (e.g., test: = ... should be test := ...) Commented Jul 13, 2013 at 18:40
  • 2
    Never use JPG for non-photographic images (such as screenshots, diagrams, logotypes, ...). Commented Jul 13, 2013 at 18:50
  • 1
    Your code makes no sense. In both examples, you tell the stream to write 1024 bytes of data but pass in about 1000 bytes too few, meaning it's just going to keep writing data past the end of the string; that random data is going to be whatever happens to be in memory past the end of the string. Pass the actual number of bytes you need to write to the stream. It doesn't work with write these bytes, and keep going for Some_Value_Plucked_From_The_Air bytes more. Read the documentation, instead of just guessing or grabbing wild values from nowhere. Commented Jul 13, 2013 at 20:33

1 Answer 1

4

You have provided non-real code, but I suspect that "here" is string. To write string body to stream, you can use the next code:

test.Write(PChar(here)^, SizeOf(Char) * Length(here));

P.S. If you are going to read this string from stream (file) later, then it would be wise to write its length before string body.

Sign up to request clarification or add additional context in comments.

5 Comments

For writing strings, it is better to use a TStreamWriter class instead. Especially on D2009+, because it is TEncoding-aware.
@RemyLebeau Can tStreamWriter write to binary?
@SHIN JaeGuk Why do you want to use instrument intended for text (TTextWriter ) for raw tasks?
@MBo Because the question is about saving binary. I would understand Remy's comment was just a general recommendation. Never mind.
@SHINJaeGuk yes. It writes to a TFileStream by default, which only knows binary. Or it can write to any TStream you give it. String writes convert characters to bytes using a specified TEncoding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.