不想让别人用即使本打开,只有自己定义文件格式,
例如:
Type
OneLine = String[191]; { 定义一行最大有191个字节 }
procedure writeOneLine(_item:string);
Var
_Line:OneLine;
TmFl : File;
begin
AssiGnFile(TmFl,'c:/oneline.bin');
If (fileexists('c:/oneline.bin')) then
begin
reset(tmfl,1);
seek(tmfl,filesize(tmfl));
end else
rewrite(tmfl,1);
_Line:=_item;
blockwrite(tmfl,_line,sizeof(OneLine));
closefile(tmfl);
end;
procedure tform1.button1click(sender:tobject);
// 把Edit1中的内容写入自定义文件中.
begin
writeOneLine(edit1.text); // 把Edit1中的内容写入自定义文件中.
end;
procedure TForm1.Button2Click(Sender: TObject);
// 把自定义文件读入memo1中.
Var
_Line:OneLine;
TmFl : File;
begin
AssiGnFile(TmFl,'c:/oneline.bin');
If (not fileexists('c:/oneline.bin')) then
begin
showmessage('file not exists!');
exit;
end else
reset(tmfl,1);
memo1.lines.clear;
while not eof(tmfl) do
begin
blockread(tmfl,_line,sizeof(OneLine));
memo1.lines.add(_line);
end;
closefile(tmfl);
end;
//这样别人用即使本打开的是乱码了!
例如读取 name=的内容.
function readitem(item:string):string;
// 把自定义文件读入memo1中.
Var
_Line:OneLine;
TmFl : File;
_pos:word;
begin
result:='空';
AssiGnFile(TmFl,'c:/oneline.bin');
If (not fileexists('c:/oneline.bin')) then
begin
showmessage('file not exists!');
exit;
end else
reset(tmfl,1);
while not eof(tmfl) do
begin
blockread(tmfl,_line,sizeof(OneLine));
_pos:=pos(uppercase(item)+'=',uppercase(_line));
if _pos<>0 then
begin
delete(_line,1,_pos+length(item+'=')-1);
result:=trim(_line);
closefile(tmfl);
exit;
end;
end;
closefile(tmfl);
end;
showmessage(readitem('NAME'));//显示name=的内容.
例如添加用
writeOneLine('name=商朝子');即可.
删除有点麻烦,但好人做到底,给你编:
function deleteitem(item:string):boolean;
// 把自定义文件读入memo1中.
Var
_Line:OneLine;
TmFl : File;
tfl : File;
_pos:word;
begin
result:=false;
AssiGnFile(TmFl,'c:/oneline.bin');
If (not fileexists('c:/oneline.bin')) then
begin
showmessage('file not exists!');
exit;
end else
reset(tmfl,1);
assignfile(tfl,'c:/oneline.1);
rewrite(tfl,1);
while not eof(tmfl) do
begin
blockread(tmfl,_line,sizeof(OneLine));
_pos:=pos(uppercase(item)+'=',uppercase(_line));
if _pos=0 then
blockwrite(tfl,_line,sizeof(OneLine));
end;
closefile(tmfl);
closefile(tfl);
erase(tmfl);
renamefile('c:/oneline.1','c:/oneline.bin');
result:=true
end;
例如删除用
deleteitem('name');即可.