Save file dialog to save txt from array of numbers

421 views Asked by At

I would save my 2 arrays of ints and floats respectively into a "csv style" .txt file using a TSaveTextFileDialog. I am trying to do that in C++ Builder with a Windows VCL application. This is my attempt:

__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);

        }

    }
}

But string has no member for "SaveToFile". How could I solve, please? Thank you in advance.

1

There are 1 answers

1
Remy Lebeau On BEST ANSWER

The dialog merely gives you filenames that the user has selected. You are responsible for actually opening those files and writing your data to them as needed.

For instance, have a look at things like TStringList::SaveToFile(), TFile::WriteAllText()/WriteAllLines(), TStreamWriter::Write()/WriteLine(), etc.

Try something like this:

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;
    }
}

Alternatively:

#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);
    }
}

Alternatively:

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;
    }
}