WriteString
-
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 { public: 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; } GeFree(charline); 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\\"); fn.SetDirectory(dir); String suffix = "txt"; fn.SetFile(String("My Text")); fn.SetSuffix(suffix); 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); }
-ScottA
-
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:
https://developers.maxon.net/forum/topic/4022/3545_getname&KW=charline&PID=14479#14479-ScottA
-
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.
-ScottA -
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; file.open(buffer); text.GetString().GetCString(buffer, sizeof(buffer)); file << buffer; file.close();
-
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:
Thanks
Will give it a try.
Peter
-
On 05/04/2013 at 17:02, xxxxxxxx wrote:
Great. This works very nicely!
Peter