簡體   English   中英

使用 TFileStream 創建一個文本文件

[英]Create a text file using TFileStream

我嘗試了以下文檔中的代碼:

讀取字符串並將其寫入文件

但它不起作用並報告編譯器錯誤:

“System::AnsiStringT<0>”中沒有名為“fmCreate”的成員

這是我嘗試過的代碼:

TFileStream *fs; const AnsiString str = "Hello";
fs = new TFileStream("temp.txt", fmCreate);
fs->Write ((void*)str.c_str(), str.fmCreate);

好吧,它看起來像一個錯字。 顯然,代碼正在嘗試將字符串寫入文件,因此需要字符串的長度。 嘗試這個

TFileStream *fs; const AnsiString str = "Hello";
fs = new TFileStream("temp.txt", fmCreate);
fs->Write ((void*)str.c_str(), str.Length());

警告,我對 VCL 一無所知。

正如約翰的回答所說,文檔中有一個錯字。 str.fmCreate應該是str.Length()

但是,有更好的方法可以將字符串寫入文本文件以避免此問題。 例如,通過使用System::Classes::TStreamWriterSystem::Ioutils::TFile代替,例如:

#include <System.Classes.hpp>

const String str = _D("Hello");
TStreamWriter *sw = new TStreamWriter(_D("temp.txt"), false, TEncoding::UTF8);
sw->Write (str);
delete sw;
#include <System.IOUtils.hpp>

const String str = _D("Hello");
TFile::WriteAllText(_D("temp.txt"), str);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM