Writing to Text Files

This brief guide discuss writing text files using C/C++.


You need to write to a text file from my general utility plugin.


Rhino C/C++ SDK does not have any special functions or classes to help you read or write text files.

With that said, there are a number of ways to read and write text files in C/C++.

  1. Use the C-runtime library’s fopen, fputs, fwrite and fclose functions.
  2. You can use the iostream library’s ofstream class.
  3. You can use Win32’s CreateFile, WriteFile, and CloseHandle functions.
  4. You can use MFC’s CFile and CStdioFile classes.


Here is a simple example that uses the C-runtime library.

  Writes a string to a text file
  text     - [in] The string to write
  filename - [in] The name of the file to write. If the file does not
                  exist, it will be created. If the file does exist,
                  it will be overwritten.
  true if successful, false otherwise.
bool WriteStringToFile( const wchar_t* text, const wchar_t* filename )
  bool rc = false;
  if( (text && text[0]) && (filename && filename[0]) )
    FILE* fp = _wfopen( filename, L"w" );
    if( fp )
      size_t num_write = wcslen( text );
      size_t num_written = fwrite( text, sizeof(wchar_t), num_write, fp );
      fclose( fp );
      rc = ( num_written == num_write );
  return rc;