I wrote below code to reformat a source text file(columns_original) and write to an output file(output.ctl). It is working but what I should do is, in the last line of output file created by below code, there is a "," at the end of the file and I want to change it to "." In which part should I embed this code? I added this at the end but I am getting an error "An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Output file is being used by another program"
// I am trying to replace "," with "." at the last line of the text I created above and I created below code.
// I am trying to replace "," with ")" at the last line of the text I created above and
using (FileStream aFile2 = new FileStream(path, FileMode.Append, FileAccess.Write))
using (StreamWriter sw2 = new StreamWriter(aFile2))
{
var lastLine = File.ReadLines(path).Last();
lastLine = lastLine.Replace(",", ".");
sw2.WriteLine(lastLine);
}
MCVE:
// I ADDED ABOVE CODE TO BELOW WORKING PART AND I AM GETTING ERROR MENTIONED IN THE POST
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace CTL_CMD_Creator
{
class Program
{
static void Main(string[] args)
{
string ColChangable1 = "VARCHAR2";
string ColChangable2 = "CHAR";
string ColChangable3 = "NUMBER";
string ColChangable4 = "DATE";
string path = @"C:\Users\***\Desktop\output.ctl";
StreamReader reader = File.OpenText(@"C:\Users\***\Desktop\Columns_Original.txt");
string line;
using (FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(aFile))
{
while ((line = reader.ReadLine()) != null)
{
string[] tokens = line.Split(new char[] { ' ', '\t', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens[1].Equals(ColChangable1) || tokens[1].Equals(ColChangable2))
{
sw.WriteLine(tokens[0] + "\t" + "\"TRIM(:" + tokens[0] + ")\",");
}
else if (tokens[1].Equals(ColChangable3))
{
sw.WriteLine(tokens[0] + "\t" + ",");
}
else if (tokens[1].Equals(ColChangable4))
{
sw.WriteLine(tokens[0] + "\t" + "DATE" + ",");
}
}
}
}
}
}
}
If I understand your problem, a simpler way would be to use System.IO.File. Demo on .NetFiddle:
using System;
using System.IO;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
// init
var path = "output.ctl";
var content =
"Hello and Welcome" + Environment.NewLine
+ "Hello and Welcome" + Environment.NewLine
+ ",";
File.WriteAllText(path, content);
var text = File.ReadAllLines(path); // read the file as string[]
foreach (var line in text) // print the file
Console.WriteLine(line);
text[text.Length - 1] = text.Last().Replace(",", "."); // replace
File.WriteAllLines(path, text); // overwrite or write to a new file
string[] lines2 = File.ReadAllLines(path); // read again
foreach (var line in lines2) // then print to show the difference
Console.WriteLine(line);
}
}
output:
Hello and Welcome
Hello and Welcome
,
Hello and Welcome
Hello and Welcome
.