拿了一个小的。是关于《用文件流从文本文件读入每个字符》的。
procedure TForm1.Button1Click(Sender: TObject);
Var FileStream:TFileStream;
Str:PChar;
i:Integer;
begin
FileStream:=TFileStream.Create('c:\Autoexec.bat',fmOpenRead); //打开文件,
try
i:=FileStream.Size; //得到文件长度
Str:=StrAlloc(i-1); //给字符串分配长度。最后一个字符是文件结尾符不需要。
FileStream.Read(Str^,i-1); //读取文件内容到字符串。
Label1.Caption:=Str;
finally
FileStream.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
Var FileStream:TFileStream;
Str:PChar;
i:Integer;
begin
FileStream:=TFileStream.Create('c:\Autoexec.bat',fmOpenRead); //打开文件,
try
i:=FileStream.Size; //得到文件长度
Str:=StrAlloc(i-1); //给字符串分配长度。最后一个字符是文件结尾符不需要。
FileStream.Read(Str^,i-1); //读取文件内容到字符串。
Label1.Caption:=Str;
finally
FileStream.Free;
end;
end;
解决方案 »
- 加密狗的 问题
- 求COM(Componet Object Model,组件对象模型)学习资料
- 请问大家一个小问题??在线等。。。。
- 求救啊,ACCESS+DELPHI,错误:'Data provider or other service returned an E_FAIL status'。
- 急问,我的DELPHI程序出了问题。
- 如何设置另一个进程中的SysListView的items的text
- 一个简单的问题,解决马上给分!给高分!!
- 大调查:关于QQ的聊天记录会不会被截获的问题?大家说说吧!
- 请问用WinExer()如何实现?
- 关于C/S的!
- 讨论: VCL是怎么现实DYNCREATE、SERIAL、persistence、reflection等的呢?
- ihihonline ihihonline ihihonline看看你的留言
Var FileStream:TFileStream;
Str:PChar;
i:Integer;
begin
FileStream:=TFileStream.Create('c:\Autoexec.bat',fmOpenRead); //打开文件,
try
i:=FileStream.Size; //得到文件长度
Str:=StrAlloc(i-1); //给字符串分配长度。最后一个字符是文件结尾符不需要。
FileStream.Read(Str^,i-1); //读取文件内容到字符串。
Label1.Caption:=Str;
finally
FileStream.Free;
end;
end;
Var FileStream:TFileStream;
Str:PChar;
i:Integer;
begin
FileStream:=TFileStream.Create('c:\Autoexec.bat',fmOpenRead); //打开文件,
try
i:=FileStream.Size; //得到文件长度
Str:=StrAlloc(i-1); //给字符串分配长度。最后一个字符是文件结尾符不需要。
FileStream.Read(Str^,i-1); //读取文件内容到字符串。
Label1.Caption:=Str;
finally
FileStream.Free;
end;
end;
是关于用流从文本文件读入字符的
读一行啊?你这是要处理文本文件?对于文本文件,用StringList最方便,用StringList的LoadFromFile把文件读入,然后用他的Strings属性操作指定的行,如:MyStringList.Strings[0] := 'This is the first string'; //把第一行改成'This is the first string'MyStringList[0] := 'This is the first string'; //也可以这样写还可以加入新行:
String aStr='你也可以直接用';
MyStringList.Add(aStr); 这样在末尾添加一句话
MyStringList.Add('你也可以直接加在这里');//跟前一句话一样的效果
MyStringList.Insert(Index,aStr); //Index是你要插入语句的地方(行号),aStr是你要插的句子删掉一行:MyStringList.Delete(Index); //Index是你要删掉的行号。最后把文件再写会文件里去MyStringList.SaveToFile(FileName);//FileName 是String类型,他指出准备写入的文件名。
如果是二进制文件,还是用TFileStream好。不过操作起来稍复杂些。:)