On 03/04/2013 at 12:46, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   12 
Platform:      Mac OSX  ; 
Language(s) :     C++  ;


I am attempting to write a very simple export plugin. The export is a text-based format and I am just starting up on the SDK functions used for writing strings to disk. When I write a regular String to disk using WriteString(String) apparently there is an extra character written before the actual character string. It appears that's the length?

What function(s) do you normally use to just write the characters to disk?



On 03/04/2013 at 12:57, xxxxxxxx wrote:

WriteString() doesn't write a regular 'string'.  It writes a C4D String (class).  You need to setup a CHAR buffer and convert the String into a CHAR* using GetCString() or use GetCStringCopy() to get a CHAR* representation.

Then you will need to write the character string character-by-character using WriteChar().  You could be adventurous and try WriteBytes() to write the entire string but read the Note in the docs about it.

On 03/04/2013 at 13:31, xxxxxxxx wrote:

Here is a simple plugin example that writes some text to a .txt file:

//This is an example plugin that writes text to a file  
#include "c4d.h"  
#include "c4d_symbols.h"  
#define PLUGIN_ID    10000010  
class SimplePlugin : public CommandData  
     SimplePlugin(); //The Constructor                  
     virtual Bool      Execute(BaseDocument *doc);  
SimplePlugin::SimplePlugin() // The Constructor  
 //not used in this example  
Bool WriteMyString(const String &line, BaseFile* file)  
  if(!file) return FALSE;  
  CHAR *charline = NULL;  
  LONG strlength = line.GetCStringLen(STRINGENCODING_7BITHEX);  
  charline = GeAllocType(CHAR, strlength+1);  
  if(!charline) return FALSE;  
  strlength = line.GetCString(charline, strlength+1, STRINGENCODING_7BITHEX);  
  LONG i;  
  for(i=0; i<strlength; i++)  
      if(!file->WriteChar(charline[i])) return FALSE;  
  return TRUE;  
Bool SimplePlugin::Execute(BaseDocument *doc)  
  AutoAlloc<BaseFile> bf; //Declare the BaseFile  
  String text = "Hello World";  
  //Set the Filename  
  Filename fn;  
  Filename dir = Filename("C:\\Users\\user\\Desktop\\");      
  String suffix = "txt";  
  fn.SetFile(String("My Text"));  
  fn.FileSelect(FILESELECTTYPE_ANYTHING, FILESELECT_SAVE, "Save Your Work"); //Opens the file browser       
  bf->Open(fn, FILEOPEN_WRITE);  
  WriteMyString(text, bf);  
return true;  
Bool RegisterSimplePlugin()  
  return RegisterCommandPlugin(PLUGIN_ID, GeLoadString(IDS_My_Simple_Plugin), 0, AutoBitmap("icon.tif"), String("Help text Goes here"), gNew SimplePlugin);                                                                        


On 03/04/2013 at 14:19, xxxxxxxx wrote:

ScottA: In your WriteChar() for-loop, you should GeFree(charline); before returning FALSE to avoid memory leaks.

On 03/04/2013 at 14:44, xxxxxxxx wrote:

I haven't used this code in a while. But I don't recall getting any memory leaks with it.
It's basically the same code Matthias posted here:


On 03/04/2013 at 15:30, xxxxxxxx wrote:

Just remember that if WriteChar() fails, then charline is not deallocated.  That is still a memory leak allbeit a less likely one.

On 03/04/2013 at 16:26, xxxxxxxx wrote:

OK. I Gotcha.
I covered a crash scenario with FALSE.  But not the memory allocated  to charline.

Thanks for the heads up.

On 03/04/2013 at 23:24, xxxxxxxx wrote:

Why not just use the standart-library?

CHAR buffer[1024];
filename.GetString().GetCString(buffer, sizeof(buffer));
ofstream file;
text.GetString().GetCString(buffer, sizeof(buffer));
file << buffer;

On 03/04/2013 at 23:33, xxxxxxxx wrote:

Remember that STL uses exceptions and the C4D SDK does not.  You can use the STL but you must handle ALL exceptions.  Any missed will drill back to c4d's main and crash C4D.  Just a word of precaution.

On 05/04/2013 at 15:08, xxxxxxxx wrote:


Will give it a try.


On 05/04/2013 at 17:02, xxxxxxxx wrote:

Great. This works very nicely!