以下是本機和託管C++的例子:
假設你很高興與本地解決了以下工作得很好:
fstream *fs =new fstream(filename,ios::out|ios::binary);
fs->write("ghgh", 4);
fs->close();
delete fs; // Need delete fs to avoid memory leak
不過,我不會用動態內存爲fstream的對象(即新的陳述和要點)。這是新版本:
fstream fs(filename,ios::out|ios::binary);
fs.write("ghgh", 4);
fs.close();
編輯,問題被編輯要求的本地解決方案(最初它不清楚),但我會離開這個答案,因爲它可能是使用的人
如果您正在尋找C++ CLI選項(用於託管代碼),我推薦使用StreamWriter而不是FileStream。 StreamWriter將允許您使用託管字符串。請注意,刪除將調用IDisposable接口的Dispose方法和收集最終將釋放內存的垃圾:
StreamWriter ^fs = gcnew StreamWriter(gcnew String(filename));
fs->Write((gcnew String("ghgh")));
fs->Close();
delete fs;
請修改您的帖子並添加您的確切錯誤消息。此外,完整的代碼(正確格式化,與標題)可能會有所幫助。 – Mat 2011-03-19 14:15:51
@user:'FileStream'從哪裏來?你爲什麼創建兩個流?爲什麼要動態創建流?你是轉換爲C++的Java程序員嗎? – 2011-03-19 14:18:14
FileStream?這是一個.NET類嗎?你想要做C++嗎?還是C++/CLI? – 2011-03-19 14:18:44