怎样处理文本的结束符。 ( 积分: 300 )

  • 主题发起人 主题发起人 陈桂坚
  • 开始时间 开始时间

陈桂坚

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样读取带有行结束符的文件?
老是只读了第一行就不读了。。
 
0x00
//有的文件中间有#00 此时Tstringlist等都要出错 #00->$20(#32)
//NoEOF('cxz.txt', 'q.txt', false);

function NoEOF(source_file_name: string; target_file_name: string; Rm_Src: boolean = False; CR: Char = #$00): string;
const
SWAP_NAME = 'D2U_';
var
swap_char: char;
s_stream, t_stream: TMemoryStream;
begin
s_stream := TMemoryStream.Create;
t_stream := TMemoryStream.Create;

try
s_stream.LoadFromFile(source_file_name);
s_stream.Position := 0;
while (s_stream.Position < s_stream.Size) do
begin
s_stream.ReadBuffer(swap_char, sizeof(swap_char));
if (swap_char = CR) then swap_char := #32;
t_stream.WriteBuffer(swap_char, sizeof(swap_char));
end; //end of while
finally
if (Rm_Src) then
begin
Deletefile(PChar(source_file_name));
result := ExtractFilePath(target_file_name) + ExtractFileName(source_file_name);
t_stream.SaveToFile(result);
end //end of if( If_Rm_Source )
else
begin
if (source_file_name <> target_file_name) then
result := target_file_name
else
result := ExtractFilePath(target_file_name) +
SWAP_NAME + ExtractFileName(target_file_name);
t_stream.SaveToFile(result);
end; //end of else
s_stream.Free;
t_stream.Free;
end; //end of finally

end;
 
你可以先读取的时候把指针移动到末尾删除结束符
 
[8D][8D][8D][8D][8D][8D][8D][8D]
代码:
 
后退
顶部