新人c#读取txt求解

MichaelGLX 2017-08-05 10:14:59
private void button5_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream("tmp1.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string strLine;
string[] ss = new string[10];
int[] s = new int[10];
while ((strLine = sr.ReadLine()) != null)
{

ss = strLine.Split(',');
s[0] = Convert.ToInt32(ss[0]);
s[1] = Convert.ToInt32(ss[1]);
s[2] = Convert.ToInt32(ss[2]);
Graphics gra = this.pictureBox1.CreateGraphics();
gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pen = new Pen(Color.Black, 3);//画笔颜色
gra.DrawEllipse(pen, s[0] - s[2], -s[1] - s[2] + 299, 2 * s[2], 2 * s[2]);//画椭圆的方法
}
sr.Close();
fs.Close();


}
读取txt格式为
100,100,100
200,100,100
200,200,100
100,200,100
10,20,10
解释为
x100,y100,r100
x200,y100,r100
x200,y200,r100
x100,y200,r100
x10,y20,r10
想要读取txt 格式为
X1329594Y1295629D100*
X1329594Y1275629D100*
X1329594Y1255629D100*
解释为x132.9594 ,y129.5629, r100保留xy保留4位小数
怎么做
...全文
211 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
MYsce 2017-08-07
  • 打赏
  • 举报
回复
引用 9 楼 MichaelGLX 的回复:
@MYsce 你这个有错误,我的意思是假如txt文件X,Y,Z有省略,则X,Y,R某值与上一行相同,但是都打印出来,你写的只能读出我给的固定这么多组,假如有很多组,怎么办?
格式任意多个: X1329594Y1295629D100* Y1275629D90* X1349594D80* X1389594* D200* X1389596D300*
string x = "";
            string y = "";
            string r = "";
            string []strArray = File.ReadAllLines("格式三.txt");
            string[] newStr = new string[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                Match mx = Regex.Match(strArray[i], @"X(\d+)");
                if (mx.Success)
                {
                    x = mx.Groups[1].Value;
                    if (x.Length > 4)
                    {
                        x = "x" + x.Insert(x.Length - 4, ".");//保留4位小数
                    }
                   
                }
                Match my = Regex.Match(strArray[i], @"Y(\d+)");
                if (my.Success)
                {
                    y = my.Groups[1].Value;
                    if (y.Length > 4)
                    {
                        y = "y" + y.Insert(y.Length - 4, ".");//保留4位小数
                    }
                   
                }
                Match mr = Regex.Match(strArray[i], @"D(\d+)");
                if(mr.Success)
                {
                    r = "r"+mr.Groups[1].Value;
                    
                }
                newStr[i] = string.Join(",", x, y, r);
}
MichaelGLX 2017-08-07
  • 打赏
  • 举报
回复
@MYsce 你这个有错误,我的意思是假如txt文件X,Y,Z有省略,则X,Y,R某值与上一行相同,但是都打印出来,你写的只能读出我给的固定这么多组,假如有很多组,怎么办?
MYsce 2017-08-05
  • 打赏
  • 举报
回复
看错题了:这才对 文件格式一: 读取txt格式为 100,100,100 200,100,100 200,200,100 100,200,100 10,20,10 解释为 x100,y100,r100 x200,y100,r100 x200,y200,r100 x100,y200,r100 x10,y20,r10

string[] strArray = File.ReadAllLines("格式1.txt");
 string[] newStr = new string[strArray.Length];
   for (int i = 0; i < strArray.Length; i++)
            {
                string[] temp = strArray[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                
                newStr[i] = string.Join(",", "x" + temp[0], "y" + temp[1], "r" + temp[2]);
            }
文件格式三: X1329594Y1295629D100* X1329594Y1275629D100* X1329594Y1255629D100* 解释为x132.9594 ,y129.5629, r100保留xy保留4位小数

 string[] strArray = File.ReadAllLines("格式3.txt");
string[] newStr = new string[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                string[] temp = strArray[i].Split(new char[] { 'X', 'Y', 'D' }, StringSplitOptions.RemoveEmptyEntries);
                double[] d = new double[2];
                for (int j = 0; j < temp.Length; j++)
                {
                    if (j < 2)
                    {
                        d[j] = Convert.ToDouble(temp[j]) / 10000;

                    }
                }
                temp[2] = temp[2].Substring(0, temp[2].Length - 1);//把最后的*去掉
                newStr[i] = string.Join(",", "x" + d[0], "y" + d[1], "r" + temp[2]);

            }
MYsce 2017-08-05
  • 打赏
  • 举报
回复
文件格式一: 读取txt格式为 100,100,100 200,100,100 200,200,100 100,200,100 10,20,10
string[] strArray = File.ReadAllLines("格式1.txt");
            string[] newStr = new string[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                IEnumerable<string> temp = strArray[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Substring(0, s.Length - 1));
                newStr[i] = string.Join(",", temp);
                    

            }
文件格式2 x100,y100,r100 x200,y100,r100 x200,y200,r100 x100,y200,r100 x10,y20,r10
string[] strArray = File.ReadAllLines("格式2.txt");
            string[] newStr = new string[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {

                IEnumerable<string> temp = strArray[i].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Substring(0, s.Length - 1));
                newStr[i] = string.Join(",", temp);
            }
文件格式三: X1329594Y1295629D100* X1329594Y1275629D100* X1329594Y1255629D100* 解释为x132.9594 ,y129.5629, r100保留xy保留4位小数
   string[] strArray = File.ReadAllLines("格式3.txt");
            string[] newStr = new string[strArray.Length];
            for (int i = 0; i < strArray.Length; i++)
            {
                string[] temp = strArray[i].Split(new char[] { 'X', 'Y', 'D' }, StringSplitOptions.RemoveEmptyEntries);
                double[] d = new double[2];
                for (int j = 0; j <temp.Length; j++)
                {
                    if(j<2)
                    {
                        d[j] = Convert.ToDouble(temp[j])/10000;
                       
                    }
                }
                temp[2] = temp[2].Substring(0, temp[2].Length - 1);//把最后的*去掉
                newStr[i] = string.Join(",", "x" + d[0], "y" + d[1], "r" + temp[2]);

            }
MYsce 2017-08-05
  • 打赏
  • 举报
回复
“文件格式三 ,y,z与上行相同的情况下: X1329594Y1295629D100* Y1275629D90* X1349594D80* X1389594* 解释为 x132.9594 ,y129.5629, r100 x132.9594 ,y127.5629,r90 x134.9594 ,y127.5629,r80 x138.9594 ,y127.5629,r80
 
           string[] strArray = File.ReadAllLines("格式三.txt");
            string[] newStr = new string[strArray.Length];
            double x=0,y=0;
           string r="";
            for (int i = 0; i < strArray.Length; i++)
            {
                string[] temp = strArray[i].Split(new char[] { 'X', 'Y', 'D','*' }, StringSplitOptions.RemoveEmptyEntries);
                
                if(i!=1)
                {
                    x = Convert.ToDouble(temp[0])/10000;//小数点四位
                    x = Math.Round(x, 4, MidpointRounding.AwayFromZero);//后面有多位保留四位多余的四舍五入
                    if (i == 0)
                    {
                        r = temp[2];
                    }
                }
                if(i==0)
                {
                    y = Convert.ToDouble(temp[1]) / 10000;
                    y = Math.Round(y, 4, MidpointRounding.AwayFromZero);
                }
                if(i==1)
                {
                    y = Convert.ToDouble(temp[0]) / 10000;
                    y = Math.Round(y, 4, MidpointRounding.AwayFromZero);
                    r = temp[1];

                }
                if(i==2)
                {
                    r = temp[1];
                }
                newStr[i] = string.Join(",", "x" + x, "y" + y, "r" + r);
            }
MichaelGLX 2017-08-05
  • 打赏
  • 举报
回复
@MYsce x,y,z与上行相同的情况下: X1329594Y1295629D100* Y1275629D90* X1349594D80* X1389594* 解释为 x132.9594 ,y129.5629, r100 x132.9594 ,y127.5629,r90 x134.9594 ,y127.5629,r80 x138.9594 ,y127.5629,r80 x,y,z与上行相同的情况下
MichaelGLX 2017-08-05
  • 打赏
  • 举报
回复
@MYsce 文件格式三: X1329594Y1295629D100* Y1275629D90* X1349594D80* X1389594* 解释为 x132.9594 ,y129.5629, r100
MYsce 2017-08-05
  • 打赏
  • 举报
回复
引用 3 楼 MichaelGLX 的回复:
@ MYsce Graphics gra = this.pictureBox1.CreateGraphics(); gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Pen pen = new Pen(Color.Black, 3);//画笔颜色 gra.DrawEllipse(pen, x,y, 2 * r, 2 * r);//画椭圆的方法 假如xyr为浮点数,该怎么做,像素点是不是只能画整数?
DrawEllipse 有几个重载 ,可以传int 的 坐标,也可以传float的坐标,其他重载你可以看下msdn说明
MichaelGLX 2017-08-05
  • 打赏
  • 举报
回复
@MYsce
MichaelGLX 2017-08-05
  • 打赏
  • 举报
回复
@ MYsce Graphics gra = this.pictureBox1.CreateGraphics(); gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Pen pen = new Pen(Color.Black, 3);//画笔颜色 gra.DrawEllipse(pen, x,y, 2 * r, 2 * r);//画椭圆的方法 假如xyr为浮点数,该怎么做,像素点是不是只能画整数?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧