/写入文件,数据集ClientDataSet1里有a,b,c三个字段
if (ClientDataSet1->IsEmpty())
{
ShowMessage("文件没有内容可以写入!");
return;
}
SaveDialog1->Title = "选择路径名:";
SaveDialog1->FileName = "模块名" + Date().FormatString("yyyymmdd");
SaveDialog1->DefaultExt = "txt";
SaveDialog1->Filter = "文本文件(*.txt)|(*.txt)|所有文件(*.*)|(*.*)";
ClientDataSet1->First();
while (!ClientDataSet1->Eof)
{
File * fp = fopen(SaveDialog1->FieldName,"wt");
fprintf(fp, "%s\t", ClientDataSet1->FieldByName("a")->AsString.Trim());
fprintf(fp, "%s\t", ClientDataSet1->FieldByName("b")->AsString.Trim());
fprintf(fp, "%s\n", ClientDataSet1->FieldByName("c")->AsString.Trim());
ClientDataSet1->Next();
}
fclose(fp);
ShowMessage("写入文件成功!");
//读取文件,存储到数据集ClientDataSet1的a,b,c字段中
int i=0,ch,ntype=0;
char str[256];
AnsiString a,b,c;
File * fp = fopen(OpenDialog->FileName,"rt");
ClientDataSet1->EmptyDataSet();
while ((ch = fgetc(fp))!=Eof)
switch(ch)
{
case '\t':
str[ntype] = '\0';
switch(i):
{
case 0: a = str;break;
case 1: b = str;break;
case 2: c = str;bread;
}
i++;
ntype = 0;
break;
case '\n':
str[ntype] = '\0';
ClientDataSet1->Insert();
ClientDataSet1->FieldByName("a")->AsString = a.Trim();
ClientDataSet1->FieldByName("b")->AsString = a.Trim();
ClientDataSet1->FieldByName("c")->AsString = a.Trim();
i = 0;
n = 0;
break;
default:
str[ntype] = ch;
ntype++;
}
fclose(fp);
ClientDataSet1->Post();
ShowMessage("读取文件成功!");