C # 保存文件和读取文件的问题

lshjune 2014-11-18 04:34:05
我想实现,根据系统时间一秒存一个数据,存成.bin格式的二进制文件,然后根据时间控件选取时间段,在读出时间,现在存的文件不对,代码如下
 
if (File.Exists(path4))
{
FileStream fs1 = new FileStream(path4, FileMode.Open, FileAccess.Write,FileShare.ReadWrite);
BinaryWriter bw1 = new BinaryWriter(fs1);
//fs1.Seek(0, SeekOrigin.End);
fs1.Seek(i, SeekOrigin.Begin);
bw1.Write(Buf_DC_A[i]);//
bw1.Flush();
bw1.Close();
fs1.Close();

}
else
{

FileStream fs1 = new FileStream(path4, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);

BinaryWriter bw1 = new BinaryWriter(fs1);
//fs1.Seek(0, SeekOrigin.End);
fs1.Seek(i, SeekOrigin.Begin);
bw1.Write(DC_A[);//

bw1.Flush();
bw1.Close();
fs1.Close();

}
,这是写文件,DC_A是数据,i是当前时间转化为的秒。

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
fs.Seek(t1, SeekOrigin.Begin);
br.Read(buf, t1, s * 4);
,这是读文件。t1是选择启示时间的秒数。
希望大家能能帮帮我,谢谢
...全文
292 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
卧_槽 2014-11-19
  • 打赏
  • 举报
回复
硬盘撑不过1个月。
卧_槽 2014-11-19
  • 打赏
  • 举报
回复
每秒写一个文件,你真对得起你的电脑。
WM_JAWIN 2014-11-19
  • 打赏
  • 举报
回复
引用 8 楼 lshjune 的回复:
[quote=引用 7 楼 bobdog19861111 的回复:] bug现象是什么?你先把语言描述好。另外你这代码风格太差了,你可以尝试使用序列化对象,多看看文件读写的章节;
没有bug现象,我单步执行时,我存的单精度数据占了六个字节,都是ASCII码,数据就不对。不知道为什么?[/quote] 固定六字节,这就简单了多了,简单说一下吧。 数据文件不存在时,新建一个文件,并设置文件大小为 518400Byte(24小时x60分x60秒x6Byte) 把日间转化为秒数,如 20:13:59 转为 (20*60+13)*60+59=72 839 把这个秒数再x6Byte, Seek(72 839 x6,SeekOrigin.Begin) 就是你要存取的位置了
bobdog19861111 2014-11-18
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace DataSaveRead
{
    [Serializable]
    public class DataClass
    {
        public int SerialNumb{get;set;}
        public int DataValue{get;set;}
        public DataClass(int n,int d)
        {
            SerialNumb = n;
            DataValue = d;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<DataClass> SaveList = new List<DataClass>();
            List<DataClass> ReadList = new List<DataClass>();
            int i=0;
            int d=100;
            int Index;
            int IndexRange;
            int ValueOfIndex;
            int[] ValueOfArray;
            Console.WriteLine("Input the file path");
            string path = Console.ReadLine() + ".txt";

            #region Save
            Console.WriteLine("***Begin to Save***");
            for (int c = 0; c < 100; c++)
            { 
                SaveList.Add(new DataClass(i++, d++)); 
            }
            using (FileStream ms = File.Create(path))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, SaveList);
            }
            Console.WriteLine("***End Save***");
            #endregion
            #region Read
            Console.WriteLine("***Begin to Read***");
            using (FileStream ms = File.OpenRead(path))
            {
                BinaryFormatter bf = new BinaryFormatter();
                object obj = bf.Deserialize(ms);
                if (obj is List<DataClass>)
                {
                    ReadList = obj as List<DataClass>;
                    #region Read one data
                    Console.WriteLine("Input the data index");
                    Index = Convert.ToInt32(Console.ReadLine());
                    var result = from n in ReadList
                                 where n.SerialNumb == Index
                                 select n.DataValue;
                    ValueOfIndex = result.ElementAt(0);
                    Console.WriteLine("Index={0},Value={1}", Index, ValueOfIndex); 
                    #endregion
                    #region Read dataArray
                    Console.WriteLine("Input the data index Range:Form 0 to ?");
                    IndexRange = Convert.ToInt32(Console.ReadLine());
                    ValueOfArray = new Int32[IndexRange];
                    var resultArray = from n in ReadList
                                      where n.SerialNumb <= IndexRange
                                      select n.DataValue;
                    Console.WriteLine("***Index Form 0 to {0} are as follow***", IndexRange);
                    ValueOfArray = resultArray.ToArray();
                    foreach (int va in ValueOfArray)
                    {
                        Console.WriteLine("Value={0}", va);
                    } 
                    #endregion
                }
                else
                {
                    return;
                }
            } 
            #endregion
            Console.ReadKey();

        }
    }
}
bobdog19861111 2014-11-18
  • 打赏
  • 举报
回复
我觉得我这个方法挺好: 1.你不用再使用seek,利用linq语句可以非常方便查找; 2.使用对象序列化,减少流操作; 3.泛型的T类型,可以根据需要,自己改DataClass类; 4.不用数据库,正好符合你的需求; 5.只是简单的记录数据、读取数据,足够了;
bobdog19861111 2014-11-18
  • 打赏
  • 举报
回复
以下建议: 1.使用对象序列化来读写文件; 2.定义一个泛型,T包含1个int号,1个int型数据; 3.每天保存一次文件流,在退出时; 4.读取文件流时,使用反序列化再转换成泛型,可利用linq在泛型中,查找index号,就找到了值; 5.以下是示例,包括了写文件,读1个值,读n个值,我已经测过没有bug了; 6.你可以参考示例后再按照你的要求修改; 7.保存数据的类必须使用特性[Serializable]; 8.我也是刚开始学C#,自己参考书写的这个例子; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Serialization.Formatters.Binary; using System.IO; namespace DataSaveRead { [Serializable] public class DataClass { public int SerialNumb{get;set;} public int DataValue{get;set;} public DataClass(int n,int d) { SerialNumb = n; DataValue = d; } } class Program { static void Main(string[] args) { List<DataClass> SaveList = new List<DataClass>(); List<DataClass> ReadList = new List<DataClass>(); int i=0; int d=100; int Index; int IndexRange; int ValueOfIndex; int[] ValueOfArray; Console.WriteLine("Input the file path"); string path = Console.ReadLine() + ".txt"; #region Save Console.WriteLine("***Begin to Save***"); for (int c = 0; c < 100; c++) { SaveList.Add(new DataClass(i++, d++)); } using (FileStream ms = File.Create(path)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, SaveList); } Console.WriteLine("***End Save***"); #endregion #region Read Console.WriteLine("***Begin to Read***"); using (FileStream ms = File.OpenRead(path)) { BinaryFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(ms); if (obj is List<DataClass>) { ReadList = obj as List<DataClass>; #region Read one data Console.WriteLine("Input the data index"); Index = Convert.ToInt32(Console.ReadLine()); var result = from n in ReadList where n.SerialNumb == Index select n.DataValue; ValueOfIndex = result.ElementAt(0); Console.WriteLine("Index={0},Value={1}", Index, ValueOfIndex); #endregion #region Read dataArray Console.WriteLine("Input the data index Range:Form 0 to ?"); IndexRange = Convert.ToInt32(Console.ReadLine()); ValueOfArray = new Int32[IndexRange]; var resultArray = from n in ReadList where n.SerialNumb < IndexRange select n.DataValue; Console.WriteLine("***Index Form 0 to {0} are as follow***", IndexRange); ValueOfArray = resultArray.ToArray(); foreach (int va in ValueOfArray) { Console.WriteLine("Value={0}", va); } #endregion } else { return; } } #endregion Console.ReadKey(); } } }
lshjune 2014-11-18
  • 打赏
  • 举报
回复
引用 7 楼 bobdog19861111 的回复:
bug现象是什么?你先把语言描述好。另外你这代码风格太差了,你可以尝试使用序列化对象,多看看文件读写的章节;
没有bug现象,我单步执行时,我存的单精度数据占了六个字节,都是ASCII码,数据就不对。不知道为什么?
bobdog19861111 2014-11-18
  • 打赏
  • 举报
回复
bug现象是什么?你先把语言描述好。另外你这代码风格太差了,你可以尝试使用序列化对象,多看看文件读写的章节;
lshjune 2014-11-18
  • 打赏
  • 举报
回复
引用 5 楼 WM_JAWIN 的回复:
你每次写的入数据是固定大么?如果不行,你这肯定做不了 在seek的时候,你要检查文件长度是否满足你的 i 条件。 fs1.Seek(i, SeekOrigin.Begin); bw1.Write(Buf_DC_A[i]);// 如果你的的buf_dc_a已经存了全部数据,你还不如直接全部重新存一次。从你这个代码上看,你的存入应该是1秒一个字节。 总之你这想法太复杂了。不如听楼上,改用数据库。
真的没有别的办法了吗??现在改用数据库晚了啊,我现在也不知道怎么办了?
WM_JAWIN 2014-11-18
  • 打赏
  • 举报
回复
你每次写的入数据是固定大么?如果不行,你这肯定做不了 在seek的时候,你要检查文件长度是否满足你的 i 条件。 fs1.Seek(i, SeekOrigin.Begin); bw1.Write(Buf_DC_A[i]);// 如果你的的buf_dc_a已经存了全部数据,你还不如直接全部重新存一次。从你这个代码上看,你的存入应该是1秒一个字节。 总之你这想法太复杂了。不如听楼上,改用数据库。
lshjune 2014-11-18
  • 打赏
  • 举报
回复
上面的 bw1.Write(Buf_DC_A[i]); bw1.Write(DC_A[); 均改为 bw1.Write(DC_A);
lshjune 2014-11-18
  • 打赏
  • 举报
回复
引用 2 楼 Z65443344 的回复:
你不如弄个数据库,检索和存储都很方便
我是想每秒存成一个数据,我是保存的一天的数据,数据库不会用,再说我的数据也不是很多啊,你能帮我看看问题出在哪里?谢谢,大神啊
於黾 2014-11-18
  • 打赏
  • 举报
回复
你不如弄个数据库,检索和存储都很方便
於黾 2014-11-18
  • 打赏
  • 举报
回复
不看你的代码 你这思路就有问题 你是想每秒存一个文件吗??????

110,539

社区成员

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

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

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