Impactnetworking


Borland C ++ Builder FAQ
The content The last update: 12/12/2008

Lines (AnsiString) and binary files

The author: trainer
The initial link: - - -

Record in the binary file
To write down AnsiString in a binary file, it needs to be transformed at first to an array char.
File recording example:
TFileStream *out = new TFileStream ("info", fmCreate);//we create a file  
if (out! = NULL) {  
   AnsiString text = "www.sources.ru";//an initial line  
   int size = text. Length (); 
   out-> Write (&size, sizeof (size));  
   if (size! = 0) out-> Write (text-> data (), size);  
} 
delete out;

Reading from the binary file
Implementation example:
AnsiString text; 
TFileStream *in = new TFileStream ("info", fmOpenRead);//we open a file  
if (in! = NULL) {  
   int size;  
   in-> Read (&size, sizeof (size));  
   if (size! = 0) {  
      try {  
         text. SetLength (size);  
         in-> Read ((void *) (text.data ()), size);  
      } catch (EOutOfMemory&) {}  
   } 
} 
delete in;  

ShowMessage (text);