0

我有一个看起来像这样的文件:

R.D.    P.N.      X       Y        Rot  Pkg
L5      120910    64.770  98.425   180  SOP8
L4      120911   -69.850  98.425   180  SOIC12
L10     120911   -19.685  83.820   180  SOIC10
L9      120911    25.400  83.820   180  0603
L5      120910    62.484  98.425   180  SOP8

Xinput我有两个标记为和的文本框Yinput。用户从这些文本框中输入值。输入值并且用户单击“GO”后,我想从文件中获取字符串并将Xinput值添加到X列中,并将Yinput值添加到Y文件中的列中。


我的意思是说...

因此,如果用户在文本框中输入“10.552”并在Xinput文本框中输入“-140.123 Yinput,新数据将如下所示:

R.D.  P.N.      X       Y         Rot  Pkg
L5    120910    75.322  -41.698   180  SOP8
L4    120911   -59.298  -41.698   180  SOIC12
L10   120911   -9.133   -56.303   180  SOIC10
L9    120911    35.952  -56.303   180  0603
L5    120910    73.036  -41.698   180  SOP8

问题:

  • 这可能吗?
  • 如果可能的话,我将如何去做?
4

4 回答 4

1

您可以使用 ADO.Net 将文件作为结构化数据读取。有大量示例可用于使用 ado.net 读取文本文件

在 ado 数据集中以结构化格式获取它后,您可以遍历和添加值。应该相当容易。

这是一篇好文章

文章链接

于 2011-07-19T17:12:18.313 回答
0

作为第一步引入单条目类,smth很明显像

 class Entry
    {
      public string Rd { get; private set; }
      public string Pn { get; private set; }
      public double X { get; set; }
      // ... declare other properties

     // Initializes a new instance of the Entry class
     public Entry(string rawData)
     {
         if (rawData == null)
         {
            throw new ArgumentNullException("rawData", "Nullable data structure can not be processed");             
         }

         string[] values = rawData.Split('\t');
         if (values == null || values.Count() != 6)
         {
            throw new ArgumentException("rawData", "Invalid data structure can not be processed");
         }

         this.Rd = values[0];
         Double.TryParse(values[2], out this.X);
         // ....
     }
    }

构建此结构后,您可以轻松地将任何值添加到 X、Y...

逐行读取文件: 来自 MSDN

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();
于 2011-07-19T17:23:04.953 回答
0

试试这个,我没有你的文件,所以这是一个最好的猜测。我确定我没有遗漏太多。我知道这似乎有点矫枉过正,但您确实可以通过这种方式完全控制数据中的每个值。

public class ComponentLocation
        {
            public string ReferenceDesignator { get; set; }

            public string PartNumber { get; set; }

            public double xValue { get; set; }

            public double yValue { get; set; }

            public int Rotation { get; set; }

            public string Package { get; set; }

        }

        public IEnumerable<ComponentLocation> ParseColumns(string fileName)
        {
            IEnumerable<string> rawData = File.ReadLines(fileName);
            var rows = rawData.Skip(1).Select(l => l.Split('\t')).Select(str => new ComponentLocation
                                                                        {
                                                                            ReferenceDesignator = str[0],
                                                                            PartNumber = str[1],
                                                                            xValue = double.Parse(str[2]),
                                                                            yValue = double.Parse(str[3]),
                                                                            Rotation = int.Parse(str[4]),
                                                                            Package = str[5]
                                                                        });
            return rows.ToList();
        }

        public void DoWork(double x, double y, string fileName)
        {
            var components = ParseColumns(fileName);
            //Add values
            foreach (var component in components)
            {
                component.xValue += x;
                component.yValue += y;
            }

            //build file
            StringBuilder sbData = new StringBuilder();
            //header
            sbData.AppendLine("R.D.\tP.N.\tX\tY\tRot\tPkg");
            //data
            foreach (var component in components)
            {
                sbData.Append(component.ReferenceDesignator).Append('\t');
                sbData.Append(component.PartNumber).Append('\t');
                sbData.AppendFormat("{0:###.###}", component.xValue).Append('\t');
                sbData.AppendFormat("{0:###.###}", component.yValue).Append('\t');
                sbData.Append(component.Rotation).Append('\t');
                sbData.Append(component.Package).Append('\t').AppendLine();
            }
            //write
            File.WriteAllText(fileName, sbData.ToString());

        }

        //call DoWork
        DoWork(10.552, -140.123, @"C:\myfile.txt")
于 2011-07-19T18:42:18.253 回答
-1

将整个文件内容读入 aStringBuilder并尝试正则表达式。

于 2011-07-19T17:48:20.400 回答