Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help about text files

se_arese_are Member Posts: 11
Hi to all!
I've got a problem and looking for suggestions.
I need to open a text file, scan it and replace ',' character with '.' character.
Could someone help me?? Thank you very much.

Sergio

Comments

  • zibadianzibadian Member Posts: 6,349
    : Hi to all!
    : I've got a problem and looking for suggestions.
    : I need to open a text file, scan it and replace ',' character with '.' character.
    : Could someone help me?? Thank you very much.
    :
    : Sergio
    :
    There are 2 ways:
    1 using textfile-typed variable
    2 using a TStrings object

    In both cases use StringReplace() to perform the actual replacement.
    1:
    Use AssignFile() and Reset() to open the file for reading, and AssignFile() and Rewrite() to open another for writing. Then use a "while not eof(SourceFile) do"-loop and Readln() to read each line individually into the memory. Replace the characters and Write() the line to the destination file. Example pseudocode:
    [code]
    // Open source file
    // Open destination file
    while not eof({Source file}) do
    begin
    Readln({Source file}, {Line});
    {Line} := StringReplace({Line}, ',', '.', [rfReplaceAll]);
    Writeln({Destination file}, {Line}
    end;
    // Close both files
    [/code]

    2:
    Create a TStringList object in memory. Call its LoadFromFile() to load the file into the object. Then StringReplace() the Text property, and finally SaveToFile() the results back to the actual file and free the object again. Example pseudocode:
    [code]
    {ListVariable} := TStringList.Create;
    {ListVariable}.LoadFromFile({Filename});
    {ListVariable}.Text := StringReplace({ListVariable}.Text, ',', '.', [rfReplaceAll]);
    {ListVariable}.SaveToFile({Filename});
    {ListVariable}.Free;
    [/code]
    The helpfiles contain more info on the various routines, types and objects.
  • se_arese_are Member Posts: 11
    Thank you very much! I've used the TStringList type, but I needed to read and save from a stream. Thank you again, have a nice day!
  • zibadianzibadian Member Posts: 6,349
    : Thank you very much! I've used the TStringList type, but I needed to read and save from a stream. Thank you again, have a nice day!
    :
    Use the LoadFromStream() and SaveToStream() methods then.
  • se_arese_are Member Posts: 11
    Yes, I did find that! The changes wera really easy... thank you again, You're really kind!!
Sign In or Register to comment.