0

There is function creating file with data in C++ Builder:

int HandleFile;
if (!FileExists(fnm))
 {HandleFile = FileCreate(fnm);FileClose(HandleFile);}

HandleFile = FileOpen(fnm,fmOpenWrite);
if(! HandleFile) {return 0;}
AnsiString str = IntToStr(num)+"#" +IntToStr( GetLastError() )+": "+ AnsiLastError();

FileSeek(HandleFile,0,2);
FileWrite(HandleFile, &str, sizeof(str));
FileClose(HandleFile);
return 1;

Is there any way to read it in python? When I open file by Notepad I see only unrecognized symbols

1 Answer 1

2
FileWrite(HandleFile, &str, sizeof(str));

isn't correct.

FileWrite expects a pointer to a raw buffer and writes x bytes of the buffer to the file given by HandleFile.

An AnsiString object contains a pointer to the heap where all data is stored (and some other variables). So sizeof(str) != str.Length() and &str != str.c_str().

You should write something like:

FileWrite(HandleFile, str.c_str(), str.Length());

Anyway take a look at TStringList, it could be what you need.

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

6 Comments

This code cannot be rewriten, and was written not by me. The data in it is in binary format! I tried to read it with numpy like: import numpy as np f = open("1.dbo", "r") a = np.fromfile(f, dtype=np.uint32) and have only integer in list. Is a way to get string where is string and int where int?
@user3216321 There aren't string / int in the file, just a sequence of bytes encoding the private data members of the AnsiString. What is the file supposed to contain?
dates and values(int,float), simbols '#', may be '()'. Dont know excactly
@user3216321 As explained, the C++ code you've posted has a serious issue and it doesn't write the content of the str string but something else.
If the code cannot be rewritten (why not?) then you are out of luck. The current code is producing a CORRUPTED file! NOTHING will be able to make sense of the file, because nothing meaningful is being written to the file in the first place. Basically, the file content is the binary numeric value of the char* pointer inside the AnsiString. The file content is not the string characters that the AnsiString points to. That pointer value is useless outside of the process that allocates the memory being pointed at.
|

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.