简体   繁体   中英

Editing Lines in a Text File Using C#

I have a slightly different problem in comparison to the other "editing text file" problems. For example, I have the following text file:

Cal Test
{
     Feature 0.0 1.0 2.0 7.5
     Feature 0.0 -1.0 2.0 -7.5
     .etc
}

Say I have about a hundred Feature lines in this text file. Essentially, the numbers represent data points; so the first two numbers represent data point: (0.0, 1.0) and the second pair represent the second data point: (2.0, 7.5) ; which represents a line in another program that takes in the text file.

What I want to do, is rotate each data point 90 degrees counter-clockwise; which means (a, b)(b, -a) . I know how I can do this, going through reading line by line and editing the points, I would like to know, is there a more efficient way to swap (a, b) and attach the negative?

As previously said, not a big improvement, but

Use ReadLines() to get the list of items, as you are probably doing on a

var list = new List<string>(); // or whatever structure you use to keep items in memory

foreach(var line in File.ReadLines(anInFileStr))
{
  var lineArray = line.Split(' ') ;
  list.Add(string.format("{0} {2} {1} {3}", lineArray[0], -int.Parse(lineArray[1]), lineArray[2], lineArray[3]));
}

File.WriteAllLines(anOutFile, list)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM