Author: Jonas Bilinkevicius
I have a disk file with lines of 80 character length. I want to concatenate all
these lines in one and later split them again into 80 character lines. How can I do
this?
Answer:
1 function GetOneString(const FilePath: string): string;
2 var3 List: TStringList;
4 i: Integer;
5 begin6 Result := '';
7 List := TStringList.Create;
8 try9 try10 list.LoadFromFile(FilePath);
11 except12 end;
13 for i := 0 to List.Count - 1 do14 Result := Result + List[i];
15 finally16 list.free;
17 end;
18 end;
To save to a file:
19 procedure SaveStrings(const FilePath: string; const Num: Integer; St:
20 string);
21 {FilePath: Name of file to save string / Num: Width of strings, 80 in your case / 22 St: String to save}23 var24 f: system.text;
25 i: Integer;
26 begin27 assignfile(f, FilePath);
28 rewrite(f);
29 i := 0;
30 while ((i + Num) <= Length(st)) do31 begin32 writeln(f, copy(st, i + 1, Num));
33 inc(i, Num);
34 end;
35 inc(i);
36 if (i < Length(st)) then37 begin38 Writeln(f, copy(st, i, Num));
39 end;
40 closefile(f);
41 end;
A TStringList has some properties like Text and Commatext which return the complete strings separated by CRLF (CarriageReturn and Line Feed) and ',' respectively, therefore you could use any of these to access the strings without wasting extra resources.