Skip to main content
Legend
December 11, 2025
Answered

How to Read and Write Text Files Using the C++ SDK

  • December 11, 2025
  • 2 replies
  • 245 views

How to Read and Write Text Files Using the C++ SDK.

I also want to know how to obtain the path to the folder containing my own PlugIn.

Correct answer Dirk Becker

Have a look at IPluginList::GetPathName if you still need your plugin path.

 

ScriptData knows how to pass an IDFile, and RunScriptParams knows how to pass a list of ScriptData. You'd then pick it up from "arguments" array, but beware - UXP chose a more convoluted way.

 

Not sure what you're doing with the stream, but in general separate execution paths for read + write can get avoided.

2 replies

Dirk BeckerCorrect answer
Legend
December 16, 2025

Have a look at IPluginList::GetPathName if you still need your plugin path.

 

ScriptData knows how to pass an IDFile, and RunScriptParams knows how to pass a list of ScriptData. You'd then pick it up from "arguments" array, but beware - UXP chose a more convoluted way.

 

Not sure what you're doing with the stream, but in general separate execution paths for read + write can get avoided.

Legend
December 11, 2025

Have a look at IDFile, AFile and related cross platform file utils / helpers. For actual access open a termnal and type "man stdio", in other words that's territory of OS and C++ standard library. boost and ICU are also in the SDK, they may help to get beyond ASCII / latin encodings.

Legend
December 12, 2025

I tried various approaches.

// We cannot be held responsible for any problems that may occur.
// Please try it at your own risk.
do
{
	IDFile iDFile;
	// AppLocalCachedDataFolder
	if (FileUtils::GetAppLocalCachedDataFolder(
		&iDFile, PMString("MyPlugInCache.txt")) == kFalse) break;

	// Write
	InterfacePtr<IPMStream> iPMStream(StreamUtil::CreateFileStreamWrite(iDFile, kOpenOut | kOpenTrunc));

	// Read
	//InterfacePtr<IPMStream> iPMStream(StreamUtil::CreateFileStreamRead(iDFile));

	if (iPMStream == nil) break;

	std::vector<ActionID> vector_actionID;
	int32 int32_size = 0;
	if (iPMStream->IsWriting()) // Write
	{
		vector_actionID.push_back(273);
		vector_actionID.push_back(271);

		// Size must be specified
		for (ActionID actionID : vector_actionID)
		{
			int32_size++;
		}

		// Write size
		iPMStream->XferInt32(int32_size);

		// Write actionID
		for (ActionID actionID : vector_actionID)
		{
			iPMStream->XferID(actionID);
		}
	}
	else if (iPMStream->IsReading()) // Read
	{
		// Query size
		iPMStream->XferInt32(int32_size);

		// Read actionID
		for (int32 i = 0; i < int32_size; i++)
		{
			ActionID actionID;
			iPMStream->XferID(actionID);
			vector_actionID.push_back(actionID);
		}
	}

	// Check
	if (iPMStream->IsReading())
	{
		for (ActionID actionID : vector_actionID)
		{
			PMString num;
			num.AsNumber(actionID.Get());
			CAlert::InformationAlert(num);
		}
	}

	// Close
	iPMStream->Flush();
	iPMStream->Close();

	// Open folder
	InterfacePtr<IScriptManager> iScriptManager(
		Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss)
	);
	if (iScriptManager == nil) break;

	InterfacePtr<IScriptEngine> iScriptEngine(
		iScriptManager->QueryDefaultEngine()
	);
	if (iScriptEngine == nil) break;

	InterfacePtr<IScriptRunner> iScriptRunner(iScriptEngine, UseDefaultIID());
	if (iScriptRunner == nil) break;

	PMString pMString_path;
	FileUtils::GetPathOnly(iDFile, pMString_path);

	RunScriptParams runScriptParams(iScriptRunner);
	ErrorCode errorCode = iScriptRunner->RunScript(
		"Folder(\""+ FileUtils::PMStringToFileURL(pMString_path) + "\").execute();"
		, runScriptParams
	);

} while (false);

 

I'm starting to think it might be easier to run the script from the C++ SDK and save it that way.