读写文本文件的小问题(30分)

  • 主题发起人 主题发起人 carrie4002
  • 开始时间 开始时间
C

carrie4002

Unregistered / Unconfirmed
GUEST, unregistred user!
调用某过程时,先判断某文件名是否存在于某目录下,如果存在则删除之,再以新的数据重新写该文件;如果该文件名不存在,则建立该文件,并把新数据写入该文件。
怎么实现?
 
文本文件不大就这样来做:
{参数说明:
FileName:文本文件名
Content: 文本文件内容
}
procedure ToFile(FileName: string; Content: string);
var
s: TStrings;
begin
s := TStringList.Create;
s.Text := Content;
s.SaveToFile(FileName);
s.Free;
end;
事先得先把要写入文本的内容写在一个字符串里,有回车换行照样写
 
procedure ToFile(FileName: string; Content: string);
var
s: TStrings;
begin
s := TStringList.Create;
s.Text := Content;
s.SaveToFile(FileName);
s.Free;
end;
 
判断文件是否存在,存在则删除
if FileExists(文件名) then
DeleteFile(文件名);
 
如果,还需要搜索文件所在的目录呢?搜索内容包括目录和文件名。
该如何写?
 
转:
在应用实践中,我们经常会用到文件查询功能。通过Win95中提供的懳募?檎覓,我们可以
方便的找出磁盘上任何子目录下的文件,其原因是该查找功能可以遍历指定目录下的所有子
目录中的文件。从编程角度讲,它实现了子目录级的文件查询。

其实,这项功能并不难实现,关键是能理解并掌握懙莨閽这种程序设计思路。本人用Delphi
实现了该项功能(任意子目录级),由于使用了懙莨閽,程序思路清晰,代码量小。

实现方法:

1. 获取当前目录下的所有下一级子目录,2. 存入字符串列表中(Tstrings)。

其中,用到了几个API函数。

FindFirst是找出指定目录下第一个文件或目录。

FindNext一般和FindFirst配合使用,用来找出下一个文件或目录。

FindClose用来关闭查询。

(以上函数Delphi在线帮助中有详尽解释,在此不赘述);

3. 用FileExists函数查找当前目录,4. 寻找是否有满足条件的文件存在。

5. 依次使各个子目录成为当前目录,6. 递归调用本函数。

7. 释放资源,8. 返回查询结果。

 

 

代码如下:

1. 从搜索记录中判断是否是子目录。

 

function IsValidDir(SearchRec:TSearchRec):Boolean;

begin


if (SearchRec.Attr=16) and

(SearchRec.Name<>'.') and

(SearchRec.Name<>'..') then

Result:=True

else

Result:=False;

end;

2. 这是查询主体函数。

参数介绍:

Mainpath: 指定的查询目录。

Filename: 欲查询的文件。

Foundresult: 返回的含完整路径的匹配文件(可能有多个)。

如果有匹配文件,函数返回True,否则,返回False;

 

function SearchFile(mainpath:string;filename:string;

var foundresult:TStrings):Boolean;

var

i:integer;

Found:Boolean;

subdir1:TStrings;

searchRec:TsearchRec;

begin

found:=false;

if Trim(filename)<>'' then

begin

subdir1:=TStringList.Create;//字符串列表必须动态生成

//找出所有下级子目录。

if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then

begin

if IsValidDir(SearchRec) then

subdir1.Add(SearchRec.Name);

while (FindNext(SearchRec) = 0) do

begin

if IsValidDir(SearchRec) then

subdir1.Add(SearchRec.Name);

end;

end;

FindClose(SearchRec);

//查找当前目录。

if FileExists(mainpath+filename) then

begin

found:=true;

foundresult.Add(mainpath+filename);

end;

//这是递归部分,查找各子目录。

for i:=0 to subdir1.Count-1 do

found:=Searchfile(mainpath+subdir1.Strings+

'/',Filename,foundresult)or found;

//资源释放并返回结果。

subdir1.Free;

end;

result:=found;

end;

 

总之,只要掌握了思路,用哪种编程语言都可以实现。现在,你可以轻松的给你的系统挂上
一个非常使用的功能了。
 
function fileQuery(
const currDir:string;{目录名}
currExt:string;{扩展名}
const treeFlg,SortFlg:boolean;{是否搜索子目录,是否排序}
const fullFlg:boolean=true):string;{是否返回全路径}
const
strLine:string=#13#10
var
recFile :TSearchRec;
strTemp :string;
FlgTemp,intLoop :Integer;
begin{返回指定目录及其子目录下指定扩展名的文件列表}
result := '';
if(currDir = '') then exit;
if(currExt = '') then currExt := '*.*';
with TStringList.Create do
try
sorted := SortFlg;
Text := strTrue(pathQuery(currDir, treeFlg,SortFlg), currDir, treeFlg);
for intLoop:=0 to Count-1 do
begin
strTemp := '';
FlgTemp := FindFirst(strings[intLoop] +currExt, faAnyFile, RecFile);
while (FlgTemp=0) do
begin
if(RecFile.Attr and faDirectory =0) then
strTemp := strTemp +strTrue(strings[intLoop],'',fullFlg) +recFile.Name +strLine;
FlgTemp := FindNext(recFile);
end;
FindClose(recFile);
{排序}
with TStringList.Create do
try
sorted := SortFlg;
Text := strTemp;
result := result +Text;
finally
Free;
end;
end;
finally
Free;
end;
end;
 
procedure fileNoted(
const fileStr,noteStr:string;{文件名及文件内容}
const slipFlg:boolean=true);{是否加入时间}
var
LogFile :TextFile;
begin{保存信息到日志文件}
AssignFile(LogFile, fileStr);
case FileExists(fileStr) of
true: Append(LogFile);{此处改为deletefile即可删除文件}
else Rewrite(LogFile);
end;
try
{加入时间,可去除}if slipFlg then Writeln(LogFile, Now);
writeln(LogFile, noteStr);
{加入分隔,可去除}if slipFlg then writeln(LogFile, '--------------------------------------------------');
finally
Closefile(LogFile);
end;
end;
 
procedure Button_Click;
var
intLoop:integer;
begin
with TStringList.Create do
try
Text := fileQuery('.', '*.ini', true, true);
for intLoop:=0 to Count-1 do
begin
deletefile(strings[intLoop]);
end;
finally
Free;
end;
fileNoted('c:/test.ini'. '测试完毕!');
end;
 
谢谢各位。
如果已知具体的插入位置,那么,可否知道到该位置号,然后用指令直接将需要替换的内容覆盖写入到该位置呢?
我不熟悉文本文件操作的函数。
 

Similar threads

后退
顶部