[CSVファイルを読み書きする]

4項目のデータをカンマで区切ったCSVファイルを読ませ、項目の順番を逆にして、CSVファイルに書き出します。

注意事項:
CSVデータをカンマで分解するのに、TStringListを使用していますが、ちょっと癖があるので注意が必要です。

読み込み時
 「カンマ」の他に「空白」も区切り文字として扱います。したがって、「空白」を含む文字列は、「""」でしっかり囲みましょう。
 Excelで作成したCSVファイルを取り込む際は、要注意です。
 
書き出し時
 空白を含まない文字列は「""」で囲って出力しません。 文字列は全て「""」で囲まれないことを認識しておく必要があります。

【ソースコード】 [tips0052.pas]
program tips0052;
{$APPTYPE CONSOLE}
uses SysUtils, Classes, MMSystem;
var
 SF,DF:TextFile;
 SL,SL2:TstringList;
 buf:string;
 cnt1,cnt2:Integer;
 tm:int64;
begin
  tm:=timeGettime; //時間の測定の開始
  WriteLn('◆ReadLn・WriteLnで1行単位で読み書き');
  AssignFile(SF,'tips0052_input.txt');
  AssignFile(DF,'tips0052_output.txt');
  SL:=TStringList.Create;
  SL2:=TStringList.Create;
  Reset(SF);
  Rewrite(DF);
  cnt1:=0; cnt2:=0;
  WriteLn('Delimiter:',SL.Delimiter);
  WriteLn('QuoteChar:',SL.QuoteChar);
  while not eof(SF) do
    begin
    ReadLn(SF,buf);
     cnt1:=cnt1+1;
    SL.DelimitedText:=buf;
    SL2.Clear;
    SL2.Add(SL.Strings[3]);
    SL2.Add(SL.Strings[2]);
    SL2.Add(SL.Strings[1]);
    SL2.Add(SL.Strings[0]);
    buf:=SL2.CommaText;
    WriteLn(DF,buf);
     cnt2:=cnt2+1;
    end;
  CloseFile(SF);
  CloseFile(DF);
  SL.Free;
  SL2.Free;
  WriteLn('入力件数:',inttostr(cnt1),'件''');
  WriteLn('出力件数:',inttostr(cnt2),'件''');
  WriteLn('処理時間:',inttostr(timeGettime-tm),'ms');//かかった時間の計算
end.
【コンパイル&実行】 [tips0052.bat]
dcc32 tips0052.pas 
tips0052.exe  > tips0052.txt
pause
【入力】 [tips0052_input.txt]
1234,5678,abcd,efgh
"1234","5678","abcd","efgh"
12 34,56 78,ab cd,ef gh
"12 34","56 78","ab cd","ef gh"
【出力】 [tips0052_output.txt]
efgh,abcd,5678,1234
efgh,abcd,5678,1234
78,56,34,12
"ef gh","ab cd","56 78","12 34"
【実行結果】 [tips0052.txt]
◆ReadLn・WriteLnで1行単位で読み書き
Delimiter:,
QuoteChar:"
入力件数:4件
出力件数:4件
処理時間:4ms