我尝试了以下文档中的代码:
Reading a String and Writing It To a File
但是它不起作用,并报告编译器错误:
在“ 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::TStreamWriter
或System::Classes::TStreamWriter
,例如:
System::Ioutils::TFile
System::Ioutils::TFile