dia

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace grab
{
    public partial class Form1 : Form
    {

        string[] data = new string[5000];   //储存数据的数组
       
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = false;
            dlg.DefaultExt = ".txt";
            dlg.Filter = "OK|*.txt";

            int num = 0;            //全局变量
            int[] sum = new int[5000];
           
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in dlg.FileNames)
                {
                    StreamReader sr = new StreamReader(file);
                    string line,line1;
                    int k = 0;
                    int l = 0;
                    int m = 0;
                    int n = 0;

                    int j = 0;

                     string stra = "";    //存放每组第一行数据
                     string strb = "";           //存放每组第二行数据
                    string str = "";             //存放每组全部数据


                    int index1 = 0;
                    int index2 = 0;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Contains("V Sync Start"))    //检测第一帧的开始
                        {
                            j++;   //帧数叠加
                            num = 0; //组数置0
                        }
                        for (int i = 100; i < 101; i++)
                        {
                            if (j == i)        
                            {
                                if (line.Contains("Packed Pixel Stream"))    //识别每组数据第一行标识符
                                {  
                                    stra = line.Substring(0, 250); //从中截断该行字符串
                                    index1 = stra.LastIndexOf(",") + 1;//起始位置                                                             

                                    if (num == sum[num])  //num-1的值与sum中的值对比,有相同的就跳过
                                    {
                                        num++;
                                        continue;
                                    }    
                                        string subb = "OK";
                                        string strReplaced = line.Replace(subb, "");
                                        int mm;
                                        mm = (line.Length - strReplaced.Length) / subb.Length; //这行OK值的个数

                                        if (mm == 2)  //个数为2,则为OK
                                        {
                                            strb = line.Substring(index1);  //句首到第一个逗号的数值
                                            index2 = strb.IndexOf(",");

                                            str = line.Substring(index1, index2);// 一行的有效数值数值

                                            //textBox1.AppendText(str);
                                            //textBox1.AppendText(Environment.NewLine);
                                            //textBox1.AppendText(Environment.NewLine);
                                                                                      
                               
                                            data[num] = str;  //放入字符串数组中    

                                               sum[num] = num;   //正确的组别数
                                        }
                                        else     //NG
                                        {
                                            num++;
                                            continue;
                                        }
                                        num++;
                                     
                                }
                            }
                        }

                        if (j == 101)//读前10帧
                            break;

                         
                    }

                    string gg = string.Join("\r", data);
                    textBox1.Text = gg.ToString();
                   // textBox1.Text = j.ToString();

                    sr.Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("编辑文本文件内容禁止为空", "提示信息");
                return;
            }
            else
            {
                StreamWriter sw = File.CreateText(@".\数据保存.txt");
                sw.Close();
            }

            try
            {
                FileStream textfile = File.Open(@".\数据保存.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(textfile, Encoding.GetEncoding("GB2312"));
                sw.Write(textBox1.Text.ToString());
                //sw.WriteLine(arr);
                MessageBox.Show("文件写成功", "提示!");
                sw.Close();
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!");
            }
        }
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/zjx123/p/12740393.html
dia
今日推荐