使用TFileStream创建文本文件

问题描述 投票:0回答:2

我尝试了以下文档中的代码:

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);
c++ c++builder vcl tfilestream
2个回答
2
投票

嗯,看起来像是一个错字。显然,代码正在尝试将字符串写入文件,因此需要字符串的长度。试试这个

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

注意,我对VCL一无所知。


0
投票

正如约翰的回答所说,文档中有错字。 str.fmCreate应该改为str.Length()

但是,有更好的方法可以将字符串写入文本文件来避免此问题。例如,通过改用System::Classes::TStreamWriterSystem::Classes::TStreamWriter,例如:

System::Ioutils::TFile
System::Ioutils::TFile
© www.soinside.com 2019 - 2024. All rights reserved.