Search code examples
c++c++buildervcltfilestream

Create a text file using TFileStream


I tried code from the following documentation:

Reading a String and Writing It To a File

But it doesn't work and reports a compiler error:

No member named 'fmCreate' in 'System::AnsiStringT<0>'

Here is the code I tried:

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

Solution

  • Well it just looks like a typo. Clearly the code is trying to write the string to the file, so the length of the string is needed. Try this

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

    Caveat, I know nothing about VCL.