using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Program
{
class Program
{
// EOFフラグ
static bool isSrmEof = false;
static bool isSrtEof = false;
static void Main(string[] args)
{
// ファイルオープン
StreamReader srm = new StreamReader
(@"files\master.csv", Encoding.UTF8);
StreamReader srt = new StreamReader
(@"files\transaction.csv", Encoding.UTF8);
StreamWriter sw = new StreamWriter
(@"files\matched.csv", false, Encoding.UTF8);
// 先読みRead
string[] mRecord;
string[] tRecord;
mRecord = mRead(srm);
tRecord = tRead(srt);
// マッチング処理のループ
while (!isSrmEof || !isSrtEof)
{
// masterのみの場合
if ((!isSrmEof && isSrtEof) ||
(string.Compare(mRecord[0],tRecord[0]) < 0))
{
// 何もしない
// master読み込み
mRecord = mRead(srm);
}
// マッチした場合
else if ((!isSrmEof && !isSrtEof) &&
(string.Compare(mRecord[0],tRecord[0]) == 0))
{
// transactionが次のキーに進むまでループ
while ((!isSrtEof) &&
!(string.Compare(mRecord[0],tRecord[0]) < 0))
{
// ファイル出力
sw.WriteLine(mRecord[1] + "," + tRecord[1]);
// transaction読み込み
tRecord = tRead(srt);
}
// master読み込み
mRecord = mRead(srm);
}
// transactionのみの場合
else if ((isSrmEof && !isSrtEof) ||
(string.Compare(mRecord[0],tRecord[0]) > 0))
{
// エラー出力
Console.WriteLine("Error:" + tRecord[0] + " is tran only.");
// transaction読み込み
tRecord = tRead(srt);
}
}
// ファイルクローズ
srm.Close();
srt.Close();
sw.Close();
}
// MasterファイルRead
static string[] mRead(StreamReader srm)
{
if (srm.Peek() == -1)
{
isSrmEof = true;
return null;
}
else
{
string str = srm.ReadLine();
return str.Split(',');
}
}
// TransactionファイルRead
static string[] tRead(StreamReader srt)
{
if (srt.Peek() == -1)
{
isSrtEof = true;
return null;
}
else
{
string str = srt.ReadLine();
return str.Split(',');
}
}
}
}
コメント