I need to read FixedLenght file, edit some data inside of it and then save that file to some location. This little app which should do all this should be run every 2 hours.
This is the example of the file:
14000 US A111 78900
14000 US A222 78900
14000 US A222 78900
I need to look for data like A111 and A222, and to replace all A111 to for example A555. I have tried using TextFieldParser but without any luck... This is my code. I am able to get element of array but I am not sure what to do next...
using (TextFieldParser parser =
FileSystem.OpenTextFieldParser(sourceFile))
{
parser.TextFieldType = FieldType.FixedWidth;
parser.FieldWidths = new int[] { 6, 3, 5, 5 };
while (!parser.EndOfData)
{
try
{
string[] fields = parser.ReadFields();
foreach (var f in fields)
{
Console.WriteLine(f);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
This is solution by Berkouz, but still having issues, the items of array are not replaced in output when saved to a file. The code:
string[] rows = File.ReadAllLines(sourceFile);
foreach (var row in rows)
{
string[] elements = row.Split(' ');
for (int i = 0; i < elements.Length; i++)
{
if (elements.GetValue(i).ToString() == "A111") {
elements.SetValue("A555", i);
}
}
}
var destFile = targetPath.FullName + "\\" + "output.txt";
File.WriteAllLines(destFile, rows);
Note the line where rows[rowIndex] is assigned to. that's because of string immutability forcing replace and similar functions to have an output value(as opposed to modifying their input) that you have to assign back to your data storage(whatever it may be, an array in this case).