我的问题大致是这样的:我要把olecontainer中的内容以流的形式存到一个文件中,而且我要多次存入到该文件中,然后我再取出来,大致思路是这样的:在文件头处记录下每次存入时的一个id号,以及起始位置,数据长度,之后再取出来,但是操作总有问题,存入的时候倒是可以,但是取出的时候总是提示说"stream read error".哪位做过这方面的东西,能否给提供一下源码供参考一下。分数不是问题。解决问题一切都好说!

解决方案 »

  1.   

    以前写过的一段代码,最后100个字节记录了原来文件的长度,然后再取Procedure TFM_Main.DecomposeFile(path:string);   //分解文件
    var
      f:textfile;
      info:FILE_INFO;
      i,j:integer;
      buf:array[0..4096] of byte;
      s:integer;
      count,b:integer;
      dir,strtmp:string;
      FilebottomInfo:array[0..100] of byte;
      FileLen:Longint;
    begin
      //dir:=GetTempDirectory;
      strtmp:='             ';
      for j:=0 to 100 do
        FilebottomInfo[j]:=0;
      dir:=path;
      try
        fstream1:=tfilestream.Create(paramstr(0),fmShareDenyWrite);
        filelen:=GetFileSize(application.ExeName);
        fstream1.Seek(filelen-100,soFromBeginning);
        fstream1.Read(FilebottomInfo,sizeof(filebottominfo));
        try
          j:=0;
          while (j<10)do
          begin
            if (FilebottomInfo[j]>47) and (FilebottomInfo[j]<121) then
              strtmp[j+1]:=chr(FilebottomInfo[j])
            else
              strtmp[j+1]:=chr(32);
            j:=j+1;
          end;
           filelen:=strtoint(trim(strtmp));
        except
          exit;
        end;   
        fstream1.Seek(filelen,soFromBeginning);
        while fstream1.Position<>fstream1.Size-100 do
        begin
          fstream1.Read(info,sizeof(info));
          count:=0;
          assignfile(f,dir+info.filename);
          rewrite(f);
          closefile(f);
          fstream2:=tfilestream.Create(dir+info.filename,fmopenwrite);
          fstream2.Size:=0;
          i:=info.len div sizeof(buf);
          for b:=1 to i do
          begin
          s:=fstream1.Read(buf,sizeof(buf));
          fstream2.Write(buf,s);
          inc(count,s);
          end;
          s:=fstream1.Read(buf,info.len-count);
          fstream2.Write(buf,s);
          fstream2.Free;
        end;
      finally
        fstream1.Free;
      end;
    end;