C++ Builder OpenDialog和SaveDialog怎么进行文件操作

Baby_2007 2008-03-27 09:25:36
我是初学者,对读取与存储不懂
...全文
1989 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Baby_2007 2008-03-28
  • 打赏
  • 举报
回复
非常感谢你们的帮忙,开始有点不懂,现在认识更深一些了。
ydlchina 2008-03-28
  • 打赏
  • 举报
回复

//OpenDialog
/*
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
*/

#include <stdio.h> // for FILE

void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *stream;
char FirstLine[512];

OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
OpenDialog1->FilterIndex = 2; // start the dialog showing all files
if (OpenDialog1->Execute())
{
for (int I = 0; I < OpenDialog1->Files->Count; I ++)
{
stream = fopen(OpenDialog1->Files->Strings[I].c_str(), "r");
if (stream)
{
// read the first line from the file
fgets(FirstLine, sizeof(FirstLine), stream);
Memo1->Lines->Append(FirstLine);
fclose(stream);
}
}
}
}
/*
This example uses a tab control to display the contents of
several files. To run the example, place a tab control on a
form and add a memo control that fits into its client area.
Be sure to leave enough room for the tabs when they appear.
Then add an OpenDialog and a button to the form. The
"Add a file" button adds a single file to the tabcontrol.
The "Assign files" button removes previous files and can be
used to assign multiple files. To assign multiple files,
use CNTL Select or SHIFT Select to select files in the
OpenDialog.
*/
void __fastcall TForm1::Add_a_fileClick(TObject *Sender)
{
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
if (OpenDialog1->Execute())
{
int index = TabControl1->Tabs->Add(OpenDialog1->FileName);
Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[index]);
}
}

void __fastcall TForm1::Assign_filesClick(TObject *Sender)
{
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
if (OpenDialog1->Execute())
{
TabControl1->Tabs->Assign(OpenDialog1->Files);
Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[TabControl1->TabIndex]);
}
}
/*
Place the following code in the tab control’s OnChange event
handler:
*/
void __fastcall TForm1::TabControl1Change(TObject *Sender)
{
Memo1->Lines->LoadFromFile(
TabControl1->Tabs->Strings[TabControl1->TabIndex]);
}
//SaveDialog
if (SaveDialog1->Execute())
Memo1->Lines->SaveToFile(SaveDialog1->FileName);

helenhf 2008-03-27
  • 打赏
  • 举报
回复
例如:点击按钮Button3,将Memo1中的内容通过SaveDialog1写到对应的文件里去

void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
Memo1->Lines->SaveToFile(SaveDialog1->FileName);
}
}

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧