Read a text file

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 20/11/2009 at 18:39, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   10/11 
Platform:   Windows  ;   Mac OSX  ; 
Language(s) :     C++  ;

---------
Hi,

I have some problems with a pretty simple task: Reading a text file and writing its contents into another file. Here's some code:

  
void CopyA2B(BaseFile *source, BaseFile *target)   
{   
     if (!source || !target) return;   
  
     CHAR c;   
     String cs = String();   
     String Content = String();   
     VLONG length = source->GetLength();   
  
     for (VLONG i = 0; i < length; i++) {   
          source->ReadChar(&c;);   
          cs.SetCString(&c;);   
          Content = Content + cs;   
     }   
     // WriteString() function posted in C4Dcafé by Matthias Bober   
     // It works fine, so the problem must be the reading   
     WriteString(Content, target);   
}   

The strange thing is, that - no matter which StringEncoding I use in SetCString() - I end up with something that looks like this:

b\u00CC\u00CC\u00CCo\u00CC\u00CC\u00CCd\u00CC\u00CC\u00CCy\u00CC\u00CC\u00CC \u00CC\u00CC\u00CC{\u00CC\u00CC\u00CC
\u00CC\u00CC\u00CC
\u00CC\u00CC\u00CC     \u00CC\u00CC\u00CCb\u00CC\u00CC\u00CCa\u00CC\u00CC\u00CCc\u00CC\u00CC\u00CCk\u00CC\u00CC\u00CCg\u00CC\u00CC\u00CCr\u00CC\u00CC\u00CCo\u00CC\u00CC\u00CCu\u00CC\u00CC\u00CCn\u00CC\u00CC\u00CCd\u00CC\u00CC\u00CC-\u00CC\u00CC\u00CCc\u00CC\u00CC\u00CCo\u00CC\u00CC\u00CCl\u00CC\u00CC\u00CCo\u00CC\u00CC\u00CCr\u00CC\u00CC\u00CC:\u00CC\u00CC\u00CC#\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CCA\u00CC\u00CC\u00CC;\u00CC\u00CC\u00CC
\u00CC\u00CC\u00CC
\u00CC\u00CC\u00CC}\u00CC\u00CC\u00CC

How can I simply read a text file using the BaseFile class and get the file contents as a String?

Thanks in advance 🙂

Cheers,
Jack

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 21/11/2009 at 03:23, xxxxxxxx wrote:

Originally posted by xxxxxxxx

User Information:
Cinema 4D Version:   10/11 
Platform:   Windows  ;  
Mac OSX  ; 
Language(s) :    
C++  ;

---------
   Hi,

I have some problems with a pretty simple task: Reading a text file and writing its contents into another file. Here's some code:

Looking at the output (it is the typical pattern of an uninitialized memory block), I assume your source->ReadChar() returned an error (you didn't check for success after calling the function) for whatever reason.

Best regards,

Wilfried Behne

Btw.: Your read loop is not that efficient. Every String addition/SetCString will create/release memory blocks.

Think about reading a larger chunk of bytes into a buffer and convert them with one call...

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 21/11/2009 at 03:34, xxxxxxxx wrote:

Thanks. Did that and it works now 🙂

Cheers,
Jack