It is an interesting problem. Please check the solution. It is not optimized properly.
Firstly, we create a simple FileStructure Class to represent the String:
public class DefaultFileStructure
{
public string FileId;
public decimal FMR;
public decimal FNMR;
public int Score;
public bool Conformity;
}
define the constant keyname for parsing .
private static string DEFAULT_KN = "tv_rocscores_DeDeP";
private static string TEST_KN = "tv_rocscores_FrFrP";
Now, Parse the file and store the data in list structure.
private List<DefaultFileStructure> GetFileStructure(string filePath, string keyName)
{
List<DefaultFileStructure> _defaultFileStructure = new List<DefaultFileStructure>();
if(!File.Exists(filePath))
{
Console.WriteLine("Error in loading the file");
}else{
string[] readText = File.ReadAllLines(filePath);
foreach (string s in readText)
{
_defaultFileStructure.Add(ParseLine(s, keyName));
}
}
return _defaultFileStructure;
}
private DefaultFileStructure ParseLine(string Line, string Keyname)
{
DefaultFileStructure _dFileStruc = new DefaultFileStructure();
string[] groups = Line.Split(new[] { ' ', ' ' },StringSplitOptions.RemoveEmptyEntries);
/* -- Format Strucure, if the log provide same format always..
Can also implement Expando concepts of C# 5.0 ***
0[tv_rocscores_DeDeP005M3TSub.csv]
1[FMR:]
2[0.0009]
3[FNMR:]
4[0.023809524]
5[SCORE:]
6[-4]
7[Conformity:]
8[True]
*/
_dFileStruc.FileId = groups[0].Replace(Keyname, "");
_dFileStruc.FMR = decimal.Parse(groups[2]);
_dFileStruc.FNMR = decimal.Parse(groups[4]);
_dFileStruc.Score = int.Parse(groups[6]);
_dFileStruc.Conformity = bool.Parse(groups[8]);
return _dFileStruc;
}
To match the difference and get the defined result as per your question.
public void getDiff(String FirstFile, string SecondFile, string ResultFile)
{
try
{
//check if file exits....
if (!File.Exists(FirstFile)) { return; }
if (!File.Exists(SecondFile)) { return; }
//Keep the result String..
StringBuilder ResultBuilder = new StringBuilder();
//Get the List of default file.
List<DefaultFileStructure> DefaultList = GetFileStructure(FirstFile, DEFAULT_KN);
//Get the List of test file.
List<DefaultFileStructure> TestList = GetFileStructure(SecondFile, TEST_KN);
//Get the diff and save in StringBuilder.
foreach (DefaultFileStructure defFile in DefaultList)
{
bool checkALL = false;
foreach (DefaultFileStructure testFile in TestList)
{
//Compare the file for diff.
if (defFile.FileId == testFile.FileId)
{
checkALL = false;
ResultBuilder.AppendLine(String.Format("result: {0} FMR_DIFF: {1} FNMR_DIFF: {2} SCORE_DIFF: {3}", defFile.FileId, defFile.FMR - testFile.FMR, defFile.FNMR - testFile.FNMR, defFile.Score - testFile.Score));
break;
}
else
{
checkALL = true;
}
}
if (checkALL == true)
{
ResultBuilder.AppendLine(String.Format("result: {0} FMR_DIFF: {1} FNMR_DIFF: {2} SCORE_DIFF: {3}", defFile.FileId, "N/A", "N/A", "N/A"));
}
}
//File processing completed.
using (StreamWriter outfile = new StreamWriter(ResultFile))
{
outfile.Write(ResultBuilder.ToString());
}
}
catch (Exception ex)
{
throw ex;
}
}
Call the following method.
getDiff(@"I:\Default_DeDe_operational_points_verbose.txt",
@"I:\FrFr_operational_points_verbose.txt",
@"I:\Result.txt");
Thanks, Ajit