保存文件对话框以从数字数组中保存 txt
Save file dialog to save txt from array of numbers
我会使用 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);
}
}
}
但是 string 没有“SaveToFile”的成员。请问我该如何解决?提前谢谢你。
该对话框仅向您提供用户已选择的文件名。您负责实际打开这些文件并根据需要将数据写入其中。
例如,看看 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;
}
}
我会使用 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);
}
}
}
但是 string 没有“SaveToFile”的成员。请问我该如何解决?提前谢谢你。
该对话框仅向您提供用户已选择的文件名。您负责实际打开这些文件并根据需要将数据写入其中。
例如,看看 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;
}
}