大家好!
  现有一采集卡能够连续的采集数据,并把采集的数据保存到aaa.txt的文档里。
现在我想:aaa.txt这个文档只保存2000个数据,就是说第2001到第4000个数据覆盖前2000个数据,后2000个数据覆盖前2000个数据,只显示最新采集到的2000个数据,我该怎么写呢??谢谢大家了
以下是连续连续扫描按钮的c#代码:
private void button1_Click(object sender, EventArgs e)  //连续扫描
        {
            a = true;
            timer1.Enabled = true;
           
            if (!File.Exists(path))
            {
                // Create a file to write to.
                fs = File.CreateText(path);
             }
// Open the file to read from.
            else
            {
                fs = File.AppendText(path);
            }谢谢

解决方案 »

  1.   

    如果你的2000个指的是字节,那就直接用FileStream写入byte,当文件指针到2000了就回0即可,如果2000是行数,那么用StreamWrite类来操作即可。
      

  2.   

    谢谢楼上。
    文档里是2000行 double型数据,还是不太会用StreamWrite类来操作,楼上帮写一下呗
      

  3.   

    给你一段测试代码,看懂了就应该会写了:            string path = "c:\\test.txt";
                double value = 0;
                int line = 0;
                StreamWriter sw = new StreamWriter(path);
                for (int i = 0; i < 8100; i++)
                {
                    if (++line == 2000)
                    {
                        sw.Flush();
                        line = 0;
                        sw.BaseStream.Seek(0, SeekOrigin.Begin);
                    }
                    value = i;
                    sw.WriteLine(value);
                }
                sw.Flush();
                sw.Close();
      

  4.   

    采集卡主框架程序mainform.csusing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;namespace MainForm
    {
        public partial class MainForm : Form
        {
            private SortedList SortedListRecvData;
            private SortedList SortedListRecvGrphData;
            int nProgress;
            private long nKey;
           
            public MainForm()
            {
                SortedListRecvData = new SortedList();
                SortedListRecvGrphData = new SortedList();
                InitializeComponent();
                mTranUdp = new UdpClass.TranUdp();
                mTranUdp.InitTranUdp();
                mTranUdp.TranUdpReceivedDataEvent += new UdpClass.TranUdp.TranUdpEventHandler(Recv);
                setCtl1.TranUdEventpSentDataEvent += new SetCtl.SetCtl.TranUdEventpSentHandler(mTranUdp.SentData);
                setCtl1.DataChangEvent += new SetCtl.SetCtl.DataChangHandler(DataChang);
                nProgress = 0;
            }
            public void DataChang(string sc ,string sd)
            {
                try
                {
                    if (sc == "AidIP")
                    {
                        mTranUdp.Udp1.sRemoteIp = sd;                }
                    else if (sc == "AidPort")
                    {
                        mTranUdp.Udp1.nRemotePort = Convert.ToInt32(sd);
                    }
                    else if (sc == "SrcIP")
                    {
                        mTranUdp.Udp1.sLocalIp = sd;
                    }
                    else if (sc == "SrcPort")
                    {
                        mTranUdp.Udp1.nLocalPort = Convert.ToInt32(sd);
                    }
                }
                catch
                { }
            }
            public void Recv(long nCmd, long[] Data)
            {
                nKey = (nKey+1)& 0xffff;
               /* lock (SortedListRecvData)
                {
                     SortedListRecvData.Add(((nKey <<16)+(nCmd & 0xffff)), Data);
                }*/
                if (nCmd == 0x1111)
                {
                    lock (SortedListRecvData)
                    {
                        SortedListRecvGrphData.Add(((nKey << 16) + (nCmd & 0xffff)), Data);                }
                    //picCtl1.UdpStatetoolStripStatusLabel.Text = "等待数据... ...";
                    //picCtl1.UdptoolStripProgressBar.Value = 0;
                    nProgress = 0;
                }
                else if (nCmd == 0x1212)
                {
                    //picCtl1.UdpStatetoolStripStatusLabel.Text = "等待数据... ...";
                    //picCtl1.UdptoolStripProgressBar.Value = (int)nKey % 160;
                    nProgress = (int)nKey;
                }
                else
                {
                    lock (SortedListRecvData)
                    {
                        SortedListRecvData.Add(((nKey << 16) + (nCmd & 0xffff)), Data);
                    }
                
                }            //timer1.Enabled = true;
                //this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
                //timer1.Start();
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                long nCmd;
                long nKey;
                long[] Data;
                //timer1.Enabled = false;
                picCtl1.UdptoolStripProgressBar.Value = (int)nProgress % 160;
                if (SortedListRecvData != null || SortedListRecvData.Count != 0)
                {
                    for (; ; )// (int i = 0; i < SortedListRecvData.Count; i++)
                    {
                        lock (SortedListRecvData)
                        {
                            if (SortedListRecvData.Count == 0) break;
                            nCmd = ((long)SortedListRecvData.GetKey(0)) & 0x00ffff;
                            nKey = ((long)SortedListRecvData.GetKey(0)) >> 16 & 0x00ffff;
                            Data = (long[])SortedListRecvData.GetByIndex(0);                        SortedListRecvData.RemoveAt(0);
                            //SortedListRecvData.Clear();
                        }
                        setCtl1.RecvData(nCmd, Data);
                    }
                }
                if (SortedListRecvGrphData != null || SortedListRecvGrphData.Count != 0)
                {
                    for (; ; )// (int i = 0; i < SortedListRecvData.Count; i++)
                    {
                        lock (SortedListRecvGrphData)
                        {
                            if (SortedListRecvGrphData.Count == 0) break;
                            nCmd = ((long)SortedListRecvGrphData.GetKey(0)) & 0x00ffff;
                            nKey = ((long)SortedListRecvGrphData.GetKey(0)) >> 16 & 0x00ffff;
                            Data = (long[])SortedListRecvGrphData.GetByIndex(0);                        // SortedListRecvGrphData.RemoveAt(0);
                            SortedListRecvGrphData.Clear();
                        }                    if (nCmd == 0x1111)
                        {
                            picCtl1.RecvData(nCmd, Data);
                            picCtl1.UdptoolStripProgressBar.Value = 0;
                            picCtl1.UdpTimetoolStripStatusLabel.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");                    }/*
                   else if (nCmd == 0x1212)
                    {
                        picCtl1.UdpStatetoolStripStatusLabel.Text = "等待数据... ...";                    picCtl1.UdptoolStripProgressBar.Value =(int) nProgress%160;
                        
                    }*/
                    }
                }
            }                private void MainForm_Load(object sender, EventArgs e)
            {        }
            
            
            
        }
    }