拿了一个小的。是关于《用文件流从文本文件读入每个字符》的。
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;

解决方案 »

  1.   

    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;
      

  2.   

    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;
    是关于用流从文本文件读入字符的
      

  3.   

    『我是初学者,能不能在详细些啊,比如我想读取某一行该怎样做啊,要是修改文件内容呢?』
    读一行啊?你这是要处理文本文件?对于文本文件,用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('你也可以直接加在这里');//跟前一句话一样的效果
      

  4.   

    插入新行:
    MyStringList.Insert(Index,aStr); //Index是你要插入语句的地方(行号),aStr是你要插的句子删掉一行:MyStringList.Delete(Index); //Index是你要删掉的行号。最后把文件再写会文件里去MyStringList.SaveToFile(FileName);//FileName 是String类型,他指出准备写入的文件名。
    如果是二进制文件,还是用TFileStream好。不过操作起来稍复杂些。:)