C# Replace a part of line and Write to new txt - c#

Here the code that I create to replace some value at txt file.
I want to replace the value 0 with 3 for lines which do not start with "#".
using (StreamWriter sr = new StreamWriter(#"D:\Testing\Ticket_post_test.txt"))
foreach(string line in File.ReadLines(#"D:\Testing\Ticket_post.txt"))
{
string[] getFromLine = line.Split(' ');
if (getFromLine[0].Equals("#") == false)
{
if (getFromLine[10].Equals("0") == true) ;
(getFromLine[10]).Replace("0", "3");
}
sr.WriteLine(line);
}
Stuck at how to replace the 0 by 3 at line split[10] and write to a new txt file.
The txt file show below
*#* start time = 2021-12-03-15-14-55
*#* end time = 2021-12-03-15-15-41
*#* name = SYSTEM
bot 10 pad 11 d 4 e 6 t #0 **0** 2021-12-03-15-14-55 # - 2021-12-03-15-15-41
bot 11 pad 12 d 5 e 7 t #0 **0** 2021-12-03-15-14-55 # - 2021-12-03-15-15-41
bot 12 pad 13 d 6 e 8 t #0 **1** 2021-12-03-15-14-55 # - 2021-12-03-15-15-41
and more

Your code makes some erroneous assumptions, which I will correct here:
When you split a string using .Split or .Substring, you are not creating a little window/little windows into the original string. You are producing altogether new strings.
When you use .Replace, you are creating a new string with the altered values, not modifying the original in-place. See this question for more info on that.
This means that:
Your replace is a no-op (it does nothing of any meaning).
Your WriteLine is just writing the original line value back to the file without your changes.
We need to both fix your replace, and create the updated string to write to the file. As we are checking the value of getFromLine[10], we don't need .Replace at all, we can just set a new value:
using (StreamWriter sr = new StreamWriter(#"D:\Testing\Ticket_post_test.txt"))
{
foreach (string line in File.ReadLines(#"D:\Testing\Ticket_post.txt"))
{
string[] getFromLine = line.Split(' ');
if (getFromLine[0] != "#" && getFromLine[10] == "0")
{
getFromLine[10] = "3";
}
sr.WriteLine(String.Join(" ", getFromLine));
}
}
This isn't especially efficient, but it should get the job done. You could potentially modify it like this to avoid creating a new string when no changes have been made:
using (StreamWriter sr = new StreamWriter(#"D:\Testing\Ticket_post_test.txt"))
{
foreach (string line in File.ReadLines(#"D:\Testing\Ticket_post.txt"))
{
string[] getFromLine = line.Split(' ');
if (getFromLine[0] != "#" && getFromLine[10] == "0")
{
getFromLine[10] = "3";
sr.WriteLine(String.Join(" ", getFromLine));
}
else
{
sr.Write(line);
}
}
}
Note that you should probably also check the length of the array (i.e. if (getFromLine.Length >= 11 && getFromLine[0] != "#" && getFromLine[10] == "0") so that you don't get any IndexOutOfRangeException errors if you reach a line of the file that has less spaces than you expect (e.g. a blank line).
P.S. I've not tested this, so I've assumed that the rest of your logic is sound.

I think this simple example will suit to your working problem.
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Stack5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
ReadAndWriteTextFile();
}
private static void ReadAndWriteTextFile()
{
// Read text file
// (?<string> ...) = name it with "string" name
// ^(?!#) = select line not begin with "#"
// (\S+\s){10} = bypass 10 group strings include a visible characters (\S) and a space (\s)
string pattern = #"(?<one>^(?!#)(\S+\s){10})0(?<two>.+)";
string substitution = #"${one}3${two}";
Regex regex = new Regex(pattern);
// Change the path to your path
List<string> lines = File.ReadLines(#".\settings.txt").ToList();
for (int i = 0; i < lines.Count(); i++)
{
// Check its value
Debug.WriteLine(lines[i]);
var match = Regex.Match(lines[i], pattern);
if (match.Success)
{
string r = regex.Replace(lines[i], substitution);
// Check its value
Debug.WriteLine("Change the line {0} to: {1}",i+1,r);
lines[i] = r;
}
}
// Write text file
File.WriteAllLines(#".\settings.txt",lines);
}
}
}
I tested it in my local machine, hope this helps you!
Ask me if you need, good to you good to me : D

Related

Comparing .txt files and getting the difference [duplicate]

This question already has answers here:
Get the differences of two files
(3 answers)
Closed 6 years ago.
I'm trying to compare two text files a.txt and b.txt, I want to get the difference between the two.
a.txt is the result from yesterday.
b.txt is the current result.
The tricky thing is that I wonna find out what is missing in "b.txt" compared to "a.txt" even tho there might have been added something new in "b.txt", these new objects needs to be excluded.
The two files is not ordered so what is in index 1 in 'a.txt' can be index 2 in 'b.txt'. I'm comparing string like "mano - mathias rønnow nørtoft".
All I had tried just ends up displaying the new objects aswell.
What I've tried:
string[] File1Lines = File.ReadAllLines(path);
string[] File2Lines = File.ReadAllLines(newPath);
List<string> NewLines = new List<string>();
for (int lineNo = 0; lineNo<File1Lines.Length; lineNo++)
{
if (!String.IsNullOrEmpty(File1Lines[lineNo]) && !String.IsNullOrEmpty(File2Lines[lineNo]))
{
if(String.Compare(File1Lines[lineNo], File2Lines[lineNo]) != 0)
NewLines.Add(File2Lines[lineNo]) ;
}
else if (!String.IsNullOrEmpty(File1Lines[lineNo]))
{
}
else
{
NewLines.Add(File2Lines[lineNo]);
}
}
if (NewLines.Count > 0)
{
File.WriteAllLines(resultpath, NewLines);
}
This just gives me the file merged. Hope I've explained my self correctly.
tried this, why is that not working? it displays nothing.
List<string> a = File.ReadAllLines(path).ToList();
List<string> b = File.ReadAllLines(newPath).ToList();
List<string> copy = new List<string>(a);
foreach (string s in copy)
{
if (b.Contains(s))
{
a.Remove(s);
}
else
{
continue;
}
}
myWriter.WriteLine(a);
That really depends on how accurate you want the diff to be and how fast you want it to be.
An easy implementation would be to get all lines of both A and B, foreach line in A, if B contains that line then remove that line from both A and B once. What's left would be the lines in A but not in B or wise versa.
Note that this method does not take ordering into consideration, so
Log 1
C
B
A
and
Log 2
A
B
C
are considered identical.
List<string> A;
List<string> B;
List<string> aCopy = new List(A);
foreach(string s in aCopy)
{
if (B.Contains(s))
{
A.Remove(s);
B.Remove(s);
}
}
//Whats in A are whats missing in B
//Whats in B are whats missing in A
You can join , sort and remove the equality string with a regex command
using System;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string strFile4xf = File.ReadAllText(#"a.txt");
strFile4xf = Regex.Replace( strFile4xf, #"(.*?)\r", "$1a\r");
File.WriteAllText(#"a1.txt", strFile4xf);
string strFile4xe = File.ReadAllText(#"b.txt");
strFile4xe = Regex.Replace( strFile4xe, #"(.*?)\r", "$1b\r");
File.WriteAllText(#"b1.txt", strFile4xe);
string s4 = File.ReadAllText(#"a1.txt");
string s2 = File.ReadAllText(#"b1.txt");
string sn = string.Concat(s4, s2);
File.WriteAllText(#"join.txt", sn);
var contents = File.ReadAllLines("join.txt");
Array.Sort(contents);
File.WriteAllLines("join.txt", contents);
string strFile4x = File.ReadAllText(#"join.txt");
strFile4x = Regex.Replace( strFile4x, #"\n(.*?)a\r\n\1b\r", "");
File.WriteAllText(#"removeequal.txt", strFile4x);
var contents2 = File.ReadAllLines("removeequal.txt");
Array.Sort(contents2);
File.WriteAllLines("removeequal.txt", contents2);
string strFile4x2 = File.ReadAllText(#"removeequal.txt");
strFile4x2 = Regex.Replace( strFile4x, #"\n\r", "");
File.WriteAllText(#"blanklines.txt", strFile4x2);
}
}
this command match the repeat string \n(.*?)\r\n\1\r when this is sorted

SubString editing

I've tried a few different methods and none of them work correctly so I'm just looking for someone to straight out show me how to do it . I want my application to read in a file based on an OpenFileDialog.
When the file is read in I want to go through it and and run this function which uses Linq to insert the data into my DB.
objSqlCommands.sqlCommandInsertorUpdate
However I want to go through the string , counting the number of ","'s found . when the number reaches four I want to only take the characters encountered until the next "," and do this until the end of the file .. can someone show me how to do this ?
Based on the answers given here my code now looks like this
string fileText = File.ReadAllText(ofd.FileName).Replace(Environment.NewLine, ",");
int counter = 0;
int idx = 0;
List<string> foo = new List<string>();
foreach (char c in fileText.ToArray())
{
idx++;
if (c == ',')
{
counter++;
}
if (counter == 4)
{
string x = fileText.Substring(idx);
foo.Add(fileText.Substring(idx, x.IndexOf(',')));
counter = 0;
}
}
foreach (string s in foo)
{
objSqlCommands.sqlCommandInsertorUpdate("INSERT", s);//laClient[0]);
}
However I am getting an "length cannot be less than 0" error on the foo.add function call , any ideas ?
A Somewhat hacky example. You would pass this the entire text from your file as a single string.
string str = "1,2,3,4,i am some text,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20";
int counter = 0;
int idx = 0;
List<string> foo = new List<string>();
foreach (char c in str.ToArray())
{
idx++;
if (c == ',')
{
counter++;
}
if (counter == 4)
{
string x = str.Substring(idx);
foo.Add(str.Substring(idx, x.IndexOf(',')));
counter = 0;
}
}
foreach(string s in foo)
{
Console.WriteLine(s);
}
Console.Read();
Prints:
i am some text
9
13
17
As Raidri indicates in his answer, String.Split is definitely your friend. To catch every fifth word, you could try something like this (not tested):
string fileText = File.ReadAllText(OpenDialog.FileName).Replace(Environment.NewLine, ",");
string words[] = fileText.Split(',');
List<string> everFifthWord = new List<string>();
for (int i = 4; i <= words.Length - 1, i + 5)
{
everyFifthWord.Add(words[i]);
}
The above code reads the selected file from the OpenFileDialog, then replaces every newline with a ",". Then it splits the string on ",", and starting with the fifth word takes every fifth word in the string and adds it to the list.
File.ReadAllText reads a text file to a string and Split turns that string into an array seperated at the commas:
File.ReadAllText(OpenDialog.FileName).Split(',')[4]
If you have more than one line use:
File.ReadAllLines(OpenDialog.FileName).Select(l => l.Split(',')[4])
This gives an IEnumerable<string> where each string contains the wanted part from one line of the file
It's not clear to me if you're after every fifth piece of text between the commas or if there are multiple lines and you want only the fifth piece of text on each line. So I've done both.
Every fifth piece of text:
var text = "1,2,3,4,i am some text,6,7,8,9"
+ ",10,11,12,13,14,15,16,17,18,19,20";
var everyFifth =
text
.Split(',')
.Where((x, n) => n % 5 == 4);
Only the fifth piece of text on each line:
var lines = new []
{
"1,2,3,4,i am some text,6,7",
"8,9,10,11,12,13,14,15",
"16,17,18,19,20",
};
var fifthOnEachLine =
lines
.Select(x => x.Split(',')[4]);

out of bounds error c#

Im trying to read contents of a csv file into different variables in order to send to a web service.It has been working fine but suddenly today i got and exception.
index was outside the bounds of the array:
what Did I do wrong?
String sourceDir = #"\\198.0.0.4\e$\Globus\LIVE\bnk.run\URA.BP\WEBOUT\";
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName2 in fileEntries)
{
// read values
StreamReader st = new StreamReader(fileName2);
while (st.Peek() >= 0)
{
String report1 = st.ReadLine();
String[] columns = report1.Split(','); //split columns
String prnout = columns[0];
String tinout = columns[1];
String amtout = columns[2];
String valdate = columns[3];
String paydate = columns[4];
String status = columns[5];
String branch = columns[6];
String reference = columns[7];
}
}
It's hard to guess without even seeing the .csv file, but my first one would be that you don't have 8 columns.
It would be easier if you could show the original .csv file, and tell us where the exception pops.
edit: If you think the data is alright, I'd suggest you debugging and see what the split call returns in Visual Studio. That might help
edit2: And since you're doing that processing in a loop, make sure each row has at least 8 columns.
My money is on bad data file. If that is the only thing in the equation that has changed (aka you haven't made any code changes) then that's pretty much your only option.
If your data file isn't too long post it here and we can tell you for sure.
You can add something like below to check for invalid column lengths:
while (st.Peek() >= 0)
{
String report1 = st.ReadLine();
String[] columns = report1.Split(','); //split columns
if(columns.Length < 8)
{
//Log something useful, throw an exception, whatever.
//You have the option to quitely note that there was a problem and
//continue on processing the rest of the file if you want.
continue;
}
//working with columns below
}
Just for sanity's sake, I combined all the various notes written here. This code is a bit cleaner and has some validation in it.
Try this:
string dir = #"\\198.0.0.4\e$\Globus\LIVE\bnk.run\URA.BP\WEBOUT\";
foreach (string fileName2 in Directory.GetFiles(dir)) {
StreamReader st = new StreamReader(fileName2);
while (!sr.EndOfStream) {
string line = sr.ReadLine();
if (!String.IsNullOrEmpty(line)) {
string[] columns = line.Split(',');
if (columns.Length == 8) {
string prnout = columns[0];
string tinout = columns[1];
string amtout = columns[2];
string valdate = columns[3];
string paydate = columns[4];
string status = columns[5];
string branch = columns[6];
string reference = columns[7];
}
}
}
}
EDIT: As some other users have commented, the CSV format also accepts text qualifiers, which usually means the double quote symbol ("). For example, a text qualified line may look like this:
user,"Hello!",123.23,"$123,123.12",and so on,
Writing CSV parsing code is a little more complicated when you have a fully formatted file like this. Over the years I've been parsing improperly formatted CSV files, I've worked up a standard code script that passes virtually all unit tests, but it's a pain to explain.
/// <summary>
/// Read in a line of text, and use the Add() function to add these items to the current CSV structure
/// </summary>
/// <param name="s"></param>
public static bool TryParseLine(string s, char delimiter, char text_qualifier, out string[] array)
{
bool success = true;
List<string> list = new List<string>();
StringBuilder work = new StringBuilder();
for (int i = 0; i < s.Length; i++) {
char c = s[i];
// If we are starting a new field, is this field text qualified?
if ((c == text_qualifier) && (work.Length == 0)) {
int p2;
while (true) {
p2 = s.IndexOf(text_qualifier, i + 1);
// for some reason, this text qualifier is broken
if (p2 < 0) {
work.Append(s.Substring(i + 1));
i = s.Length;
success = false;
break;
}
// Append this qualified string
work.Append(s.Substring(i + 1, p2 - i - 1));
i = p2;
// If this is a double quote, keep going!
if (((p2 + 1) < s.Length) && (s[p2 + 1] == text_qualifier)) {
work.Append(text_qualifier);
i++;
// otherwise, this is a single qualifier, we're done
} else {
break;
}
}
// Does this start a new field?
} else if (c == delimiter) {
list.Add(work.ToString());
work.Length = 0;
// Test for special case: when the user has written a casual comma, space, and text qualifier, skip the space
// Checks if the second parameter of the if statement will pass through successfully
// e.g. "bob", "mary", "bill"
if (i + 2 <= s.Length - 1) {
if (s[i + 1].Equals(' ') && s[i + 2].Equals(text_qualifier)) {
i++;
}
}
} else {
work.Append(c);
}
}
list.Add(work.ToString());
// If we have nothing in the list, and it's possible that this might be a tab delimited list, try that before giving up
if (list.Count == 1 && delimiter != DEFAULT_TAB_DELIMITER) {
string[] tab_delimited_array = ParseLine(s, DEFAULT_TAB_DELIMITER, DEFAULT_QUALIFIER);
if (tab_delimited_array.Length > list.Count) {
array = tab_delimited_array;
return success;
}
}
// Return the array we parsed
array = list.ToArray();
return success;
}
You should note that, even as complicated as this algorithm is, it still is unable to parse CSV files where there are embedded newlines within a text qualified value, for example, this:
123,"Hi, I am a CSV File!
I am saying hello to you!
But I also have embedded newlines in my text.",2012-07-23
To solve those, I have a multiline parser that uses the Try() feature to add additional lines of text to verify that the main function worked correctly:
/// <summary>
/// Parse a line whose values may include newline symbols or CR/LF
/// </summary>
/// <param name="sr"></param>
/// <returns></returns>
public static string[] ParseMultiLine(StreamReader sr, char delimiter, char text_qualifier)
{
StringBuilder sb = new StringBuilder();
string[] array = null;
while (!sr.EndOfStream) {
// Read in a line
sb.Append(sr.ReadLine());
// Does it parse?
string s = sb.ToString();
if (TryParseLine(s, delimiter, text_qualifier, out array)) {
return array;
}
}
// Fails to parse - return the best array we were able to get
return array;
}
Since you don't know how many columns will be in csv file, you might need to test for length:
if (columns.Length == 8) {
String prnout = columns[0];
String tinout = columns[1];
...
}
I bet you just got an empty line (extra EOL at the end), and that's as simple as that

Replacing text in a file with datatable values

We have a sample text file with the text:
The things God has prepared for those who love him
We read the text into datatable and assigned some values like this:
The 1
----------
things 2
----------
God 3
----------
has 4
----------
prepared 5
----------
for 6
----------
those 7
----------
who 8
----------
love 9
----------
him 10
----------
We're trying to replace the text in the input file with these corresponding numbers.
Is it possible? If possible, how can we do it?
Edit2:
we edited our code like this:
:
void replace()
{
string s1, s2;
StreamReader streamReader;
streamReader = File.OpenText("C:\\text.txt");
StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
int x = st.Rows.Count;
int i1 = 0;
// Now, read the entire file into a string
while ((line = streamReader.ReadLine()) != null)
{
for (int i = 0; i < x; i++)
{
s1 = Convert.ToString(st.Rows[i]["Word"]);
s2 = Convert.ToString(st.Rows[i]["Binary"]);
s2+="000";
char[] delimiterChars = { ' ', '\t' };
string[] words = line.Split(delimiterChars);
// Write the modification into the same file
string ab = words[i1]; // exception occurs here
// Console.WriteLine(ab);
streamWriter.Write(ab.Replace(s1, s2));
i1++;
}
}
streamReader.Close();
streamWriter.Close();
}
but we're getting an "Array index out of bounds" exception. we're unable to find the problem.
thanks in advance
here is a bit of code to help you get going, it hasn't been extensively tested:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
File.WriteAllText("sample1.txt", "The things God has prepared for those who love him the");
string text = File.ReadAllText("sample1.txt").ToLower();
var words = text
.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Distinct()
.OrderByDescending(word => word.Length);
var values = new Dictionary<string, int>();
for (int i = 0; i < words.Count(); i++)
{
values.Add(words.ElementAt(i), i + 1);
}
foreach (var kvp in values)
{
text = text.Replace(kvp.Key, kvp.Value.ToString());
}
File.WriteAllText("sample1.txt", text);
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
}
}
it creates a test text file, reads it, converts it to lowercase, makes identifiers for distinct words, and replaces text based on those identifiers. long words are replaced before short words to offer a bit of bad replacement prevention.
UPDATE: I just noticed the question was updated and it's no longer an option to read the entire file in one string.. sigh.. so my answer only applies when you read and write all text in one go, maybe you can reuse parts of it when reading and writing per word.

C# - Read/Copy/Replace Lines In Text

I have a text file that I am opening up and it is in a similar format to this:
10 SOME TEXT
20 T A40
B B5, C45, D48
30 B E25
40 B F17, G18
60 T H20, I23,
B J6, K7, L8, M9, N10, O11, P12,
Q31, R32, S33, T34, U35, V36,
W37, X38, Y39
100 T Z65
360 B A1, B4, C5, D6, E7, F10
2000 T SOME TEXT
423 TEXT
With this text I need to be able to read it and replace values accordingly. If a ReadLine begins with a number (ie, 10, 20, 30, 40, 60, 100, 360, 2000, 423) I need to to check if there is a T, B, or text after it. The only case that I need to change/reformat the lines when they come in and output them differently.
Example: 10 is fine except for I would like to add zeros in front of every number to make them 4 digits long (ie, 10 turns to 0010, 360 turns to 0360, 2000 stays the same). When the string "B B5, C45, D48" is read (this is the third line in the text) I need to change it to say "20A B5, C45, D48". I need to grab the number above the "B" and concat it to the "B" and replace the "B" with an "A". If instead of a "B" there is a "T" I simply need to remove the "T". Also, if a line does not start with a number or a "B" (ie, Q31 or W37) I need to concat that line with the previous line.
So after the changes take place it should look like this:
0010 SOME TEXT
0020 A40
0020A B5, C45, D48
0030A E25
0040A F17, G18
0060 H20, I23,
0060A J6, K7, L8, M9, N10, O11, P12, Q31, R32, S33, T34, U35, V36, W37, X38, Y39
0100 Z65
0360A A1, B4, C5, D6, E7, F10
2000 SOME TEXT
0423 TEXT
I am currently trying to use Regex to do this but I have been told that there is an easier way to do this and I am not sure how. So far I have been able to add the zeros in front of the numbers. Also, my code is adding an "A" to the end of everything as well as keeping the original number on the next line and I am not grabbing the lines that begin with anything but a digit.
This is what my current output is turning out to look like:
0010A
0010
0020A
0020
0030A
0030
0060A
0060
0100A
0100
0360A
0360
2000
2000
0423A
0423
I am obviously doing something wrong using Regex.
Here is my current code:
private void openRefsButton_Click(object sender, EventArgs e)
{
// Initialize the OpenFileDialog to specify the .txt extension as well as
// its intial directory for the file.
openRefs.DefaultExt = "*.txt";
openRefs.Filter = ".txt Files|*.txt";
openRefs.InitialDirectory = "C:\\";
openRefs.RestoreDirectory = true;
try
{
// Open the contents of the file into the originalTextRichTextBox.
if (openRefs.ShowDialog() == DialogResult.OK && openRefs.FileName.Length > 0)
refsTextRichTextBox.LoadFile(openRefs.FileName, RichTextBoxStreamType.PlainText);
// Throws a FileNotFoundException otherwise.
else
throw new FileNotFoundException();
StreamReader refsInput = File.OpenText(openRefs.FileName);
string regExpression = #"^[\d]+";
string findNewBottomRegex = #"^B\s";
StringBuilder buildNumberText = new StringBuilder();
StringBuilder formatMatchText = new StringBuilder();
foreach (string allLines in File.ReadAllLines(openRefs.FileName))
{
Match newBottomMatch = Regex.Match(allLines, findNewBottomRegex);
Match numberStartMatch = Regex.Match(allLines, regExpression);
int counter = 0;
if (counter < numberStartMatch.Length)
{
if (numberStartMatch.Value.Length == 2)
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText("00" + numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText("00" + numberStartMatch + "\n");
}
else if (numberStartMatch.Value.Length == 3)
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText("0" + numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText("0" + numberStartMatch + "\n");
}
else
{
if (refsTextRichTextBox.Text.Contains(newBottomMatch.ToString()))
{
finalTextRichTextBox.AppendText(numberStartMatch + "A\n");
}
finalTextRichTextBox.AppendText(numberStartMatch + "\n");
}
counter++;
}
}
}
// Catches an exception if the file was not opened.
catch (Exception)
{
MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
QUESTION(S):
What is a better way to go about doing this task?
Are there any recommendations on changing my code to be more efficient and cleaner?
How do I properly split each line into number, T/B, A40 when every line is not the same?
After the lines are properly split, how do I replace copy the line before if the current line begins with a "B"?
If the line begins with "Q31" or similar, how do I add that current line to the end of the previous one?
Once this happens, is there a way to concat everything to create the speficied format above?
WORK FLOW #jaywayco
Open Text File
Read file line by line
Save each line in a list of strings
Split each string by ' '
Find each line that starts with a digit
Replace that digit to make it 4 digits in length
Check the following text after the digit to see if it is a "B ", "T ", or "SOME TEXT"
if "B " copy the line above
Add an "A" to the end of the digit
if "T " remove the "T "
if "SOME TEXT" do nothing
Find each line that starts with a "B "
Copy the digits on the line above and concat to the front of the "B "
Follow step 4.b.i
Find each line that starts with (or similar to) "Q31"
Concat this line to the end of the previous line
...?
Here's a really lame, procedural solution:
using System.IO;
using System.Collections.Generic;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var list = new List<string>();
using (var reader = File.OpenText(#"c:\input.txt"))
{
while (true)
{
var line = reader.ReadLine();
if (string.IsNullOrEmpty(line)) break;
list.Add(line);
}
}
list = HandleRemoveTRequirement(list);
list = HandleFourDigitRequirement(list);
list = HandleConcatRequirement(list);
list = HandleStartsWithBRequirement(list);
list = HandleSecondElementIsBRequirement(list);
using (var output = new StreamWriter(#"c:\output.txt"))
{
foreach (var line in list)
{
output.WriteLine(line);
}
}
}
static List<string> HandleSecondElementIsBRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[1].Equals("B"))
{
parts[0] += "A";
parts[1] = string.Empty;
result.Add(string.Join(" ", parts).Replace(" ", " "));
}
else
{
result.Add(line);
}
}
return result;
}
static List<string> HandleStartsWithBRequirement(List<string> list)
{
var result = new List<string>();
var i = 0;
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[0].Equals("B"))
{
parts[0] = string.Empty;
result.Add(list[i - 1].Split(' ')[0] + "A" + string.Join(" ", parts));
}
else
{
result.Add(line);
}
i++;
}
return result;
}
static List<string> HandleConcatRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
int test;
if (int.TryParse(parts[0], out test) || parts[0].Equals("B"))
{
result.Add(line);
}
else
{
result[result.Count -1] += line;
}
}
return result;
}
static List<string> HandleRemoveTRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
if (parts[1].Equals("T"))
{
parts[1] = string.Empty;
}
result.Add(string.Join(" ", parts).Replace(" ", " "));
}
return result;
}
static List<string> HandleFourDigitRequirement(List<string> list)
{
var result = new List<string>();
foreach (var line in list)
{
var parts = line.Split(' ');
int test;
if (int.TryParse(parts[0], out test))
{
parts[0] = parts[0].PadLeft(4, '0');
result.Add(string.Join(" ", parts));
}
else
{
result.Add(line);
}
}
return result;
}
}
}
These are pretty complicated requirements and I would be tempted to implement this as a workflow. This way you can separate out each of the logical steps and this will increase maintainability.
I would be tempted to represent the text file as an array of string arrays or even a data table. Then you can write general functions that concatenate/transform specific values
One way to possibly approach this is similiar to jaywayco's.
I'd start with placing each line split by spaces into it's own array. Place that array into an Array of arrays. From there you can consider your workflow. Your line array that is split by the spaces you can determine how to print it based off the first value, being a number or letter B etc... If it's a B, you know that it should start with array[i-1] first value, which would be the number etc. You'd have to think through the logic a bit, but I think you can understand where I am coming from. I'm not sure if this is the best approach or not, but I think this is the way I would tackle it. Good luck!
Edit: Here is some mock code...
var mainArray = new Array[textFile.Count];
//obviously get the count of number of lines set that to the size of your array object.
for(int i=0; i < mainArray.Length; i++)
{
var line = methodToGetLineFromTextFile[i];
string[] lineArray = line.Split(' ');
mainArray[i] = lineArray;
}
//Once you have everything loaded into your arrays, apply your workflow logic.
Hope this helps!
The way I would go about this task is to write a set of unit tests based on your requirements, then make them pass one at a time (having one test per requirement).
As jaywayco suggested, I would read the file into an array of lines, then implement each of your rules as a line transformation method which can be tested in isolation. I would probably separate out the method which can select which transformation(s) to apply. Then loop over the lines and apply the transformations.

Categories

Resources