I am trying to retrieve data from a file to change it and replace the old data.
I have a file that looks something like this:
TEXT NEXT_TEXT 10.505 -174.994 0
TEXT NEXT_TEXT 100.005 174.994 90
TEXT NEXT_TEXT -10.000 -5.555 180
TEXT NEXT_TEXT -500.987 5.123 270
TEXT NEXT_TEXT 987.123 1.000 180
TEXT NEXT_TEXT 234.567 200.999 90
I am trying to grab the 3rd and 4th columns to change the data based on what a user inputs into two TextBoxes
. Let's label the 3rd column "X" and the 4th column "Y". With this, there are also two TextBoxes labeled "xTextBox" and "yTextBox". These textboxes allow the user to enter in numbers and the number they enter in (postive, negative, and/or a decimal up to 3 decimal places) for each textbox (X and Y) will be ADDED to the "X" column values and the "Y" column values.
Right now, I am using this CODE (below) to strip the "X" and "Y" columns and display their values into different RichTextBoxes
labeled xRichTextBox and yRichTextBox.
So, the xRichTextBox looks like this:
10.505
100.005
-10.000
-500.987
987.123
234.567
and the yRichTextBox looks like this:
-174.994
174.994
-5.555
5-123
1.000
200.999
CODE:
private void calculateXAndYPlacementTwo()
{
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath);
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
while (true)
{
var line = fileReader.ReadLine();
if (line == null)
break;
fileList.Add(line);
}
// Creates new lists to hold certain matches for each list.
var xyResult = new List<string>();
var xResult = new List<string>();
var yResult = new List<string>();
// Iterate over each line in the file and extract the x and y values
fileList.ForEach(line =>
{
Match xyMatch = Regex.Match(line, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
if (xyMatch.Success)
{
// grab the x and y values from the regular expression match
String xValue = xyMatch.Groups["x"].Value;
String yValue = xyMatch.Groups["y"].Value;
// add these two values, separated by a space, to the "xyResult" list.
xyResult.Add(String.Join(" ", new[]{ xValue, yValue }));
// Adds the values into the xResult and yResult lists.
xResult.Add(xValue);
yResult.Add(yValue);
// Place the 'X' and 'Y' values into the proper RTB.
xRichTextBox.AppendText(xValue + "\n");
yRichTextBox.AppendText(yValue + "\n");
}
});
}
SO for example if the user enters in "10.005" for the xTextBox
and "-20" for the yTextBox
the updated xRichTextBox
would look like this:
20.510
110.010
0.005
-490.982
997.128
224.572
and the updated yRichTextBox
would look like this:
-194.994
154.994
-25.555
-14.877
-19.000
180.999
After this takes place, I am trying to replace the 3rd and 4th columns of the original file with the updated values.
- I am currently getting the values ("X" and "Y") in the file and outputting them to seperate
RichTextBoxes
, but I do not know how to add these values to the values entered in from eachTextBox
... How can I do this? - After the
RichTextBox
values are calculated with the 'TextBox` values, how can I take these new values and add them to the original file?
**These values are not always the same or else I would hardcode them in.
So, the new file would look like this:
TEXT NEXT_TEXT 20.510 -194.994 0
TEXT NEXT_TEXT 110.010 154.994 90
TEXT NEXT_TEXT 0.005 -25.555 180
TEXT NEXT_TEXT -490.982 -14.877 270
TEXT NEXT_TEXT 997.128 -19.000 180
TEXT NEXT_TEXT 224.572 180.999 90
You know how to read the files, but it sounds as though you're just putting them straight into the textboxes. I would create an object to represent a line (with an x, y, and whatever the last column is). When you read them in, create a list of the objects. To display them, loop through and select each x (can easily be done with linq if you're using 4.0) and each y for the separate ones. To update, loop through and add the appropriate number to each object. Then to save, just write out each object again.
Edit: After looking more carefully at your code, you're keeping a list of the Xs, and a list of the Ys. Create the object I mentioned and only keep a list of those.
before u save your date be sure to create string of data to be saved and than using textwriter or streamwriter save all content to the file again. I do not think u will be able to change only one portion of file and save it
I suggest using a structured text file parser to parse and rewrite the file.
There is one in the
Microsoft.VisualBasic.FileIO
namespace, theTextFieldParser
. This is a normal .NET assembly, so you can reference it in your code and use in from C#.