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

manlio
  • 18,345
  • 14
  • 76
  • 126

1 Answers1

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.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
  • 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 Jun 03 '16 at 09:31
  • @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? – manlio Jun 03 '16 at 09:57
  • dates and values(int,float), simbols '#', may be '()'. Dont know excactly – user3216321 Jun 03 '16 at 10:28
  • 1
    @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. – manlio Jun 03 '16 at 14:34
  • 2
    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. – Remy Lebeau Jun 04 '16 at 01:39
  • It's a very big project I have only some cut of source code( Fonded another part may it will clear situation:datetime++; datetime.DecodeDate(&year,&month,&day); datetime = EncodeDate(year, month, day); datetime += EncodeTime((Word)0.0, (Word)0.0, (Word)0.0, (Word)0.0); How this data coded – user3216321 Jun 09 '16 at 06:13