0

我会使用 TSaveTextFileDialog 将我的 2 个整数和浮点数组分别保存到“csv 样式”.txt 文件中。我正在尝试使用 Windows VCL 应用程序在 C++ Builder 中执行此操作。这是我的尝试:

__fastcall TForm6::TForm6(TComponent* Owner)
    : TForm(Owner)
{
    saveTxtDlg = new TSaveTextFileDialog(this);

    TStrings* encodings = new TStringList();

    encodings->AddObject("UTF8", TEncoding::UTF8);

    saveTxtDlg->Encodings->Assign(encodings);

    saveTxtDlg->Filter = "Text files (*.txt)|*.TXT";
}
void __fastcall TForm6::Button2Click(TObject *Sender)
{
    if (saveTxtDlg->Execute(this->Handle))
    {
        String filename = saveTxtDlg->FileName + ".txt";

        int encIndex = saveTxtDlg->EncodingIndex;

        TEncoding* enc = dynamic_cast<TEncoding*>
            (saveTxtDlg->Encodings->Objects[encIndex]);

        int x[] = {1, 2, 3, 4, 5};
        float y[] = {10.1, 20.5, 30.8, 40.7, 50.2};

        for (int i = 0; i < 5; i++) {
            std::string s = std::to_string(x[i]) + "," + std::to_string(y[i]);
            s.SaveToFile(filename, enc);

        }

    }
}

但是字符串没有“SaveToFile”的成员。请问我怎么解决?先感谢您。

4

1 回答 1

1

该对话框仅为您提供用户选择的文件名。您负责实际打开这些文件并根据需要将数据写入其中。

例如,看一下TStringList::SaveToFile(), TFile::WriteAllText()/ WriteAllLines(), TStreamWriter::Write()/WriteLine()等内容。

尝试这样的事情:

void __fastcall TForm6::Button2Click(TObject *Sender)
{
    if (saveTxtDlg->Execute(this->Handle))
    {
        String filename = saveTxtDlg->FileName;

        int encIndex = saveTxtDlg->EncodingIndex;

        TEncoding* enc = static_cast<TEncoding*>(saveTxtDlg->Encodings->Objects[encIndex]);

        int x[] = {1, 2, 3, 4, 5};
        float y[] = {10.1, 20.5, 30.8, 40.7, 50.2};

        TStringList *txt = new TStringList;

        for (int i = 0; i < 5; ++i) {
            String s = String(x[i]) + _D(",") + String(y[i]);
            txt->Add(s);
        }

        txt->SaveToFile(filename, enc);
        delete txt;
    }
}

或者:

#include <System.IOUtils.hpp>

void __fastcall TForm6::Button2Click(TObject *Sender)
{
    if (saveTxtDlg->Execute(this->Handle))
    {
        String filename = saveTxtDlg->FileName;

        int encIndex = saveTxtDlg->EncodingIndex;

        TEncoding* enc = static_cast<TEncoding*>(saveTxtDlg->Encodings->Objects[encIndex]);

        int x[] = {1, 2, 3, 4, 5};
        float y[] = {10.1, 20.5, 30.8, 40.7, 50.2};

        TStringDynArray txt;
        txt.Length = 5;

        for (int i = 0; i < 5; ++i) {
            String s = String(x[i]) + _D(",") + String(y[i]);
            txt[i] = s;
        }

        TFile::WriteAllLines(filename, txt, enc);
    }
}

或者:

void __fastcall TForm6::Button2Click(TObject *Sender)
{
    if (saveTxtDlg->Execute(this->Handle))
    {
        String filename = saveTxtDlg->FileName;

        int encIndex = saveTxtDlg->EncodingIndex;

        TEncoding* enc = static_cast<TEncoding*>(saveTxtDlg->Encodings->Objects[encIndex]);

        int x[] = {1, 2, 3, 4, 5};
        float y[] = {10.1, 20.5, 30.8, 40.7, 50.2};

        TStreamWriter *txt = new TStreamWriter(filename, false, enc);

        for (int i = 0; i < 5; ++i) {
            String s = String(x[i]) + _D(",") + String(y[i]);
            txt->WriteLine(s);
        }

        txt->Flush();
        delete txt;
    }
}
于 2021-11-04T00:57:26.153 回答