使用BinaryWriter和BinaryReader操作搜索c# [英] Search using BinaryWriter and BinaryReader Operation c#

查看:154
本文介绍了使用BinaryWriter和BinaryReader操作搜索c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一些像roll,name和mark这样的数据。我把这些写到文件中。喜欢

Hi,

I have some data like roll, name and mark. I have written these to a file. Like

FileStream fs = new FileStream("data.dat",FileMode.OpenOrCreate);
           BinaryWriter br = new BinaryWriter(fs);
           br.Write(Convert.ToInt32(textBox1.Text)); //Textbox1 for int roll
           br.Write(textBox2.Text);                  // Textbox2 for string name
           br.Write(Convert.ToInt32(textBox3.Text)); //Textbox3 for int mark
           fs.Flush();
           br.Flush();
           fs.Close();
           br.Close();



我们每次都可以使用BinaryWriter在新行中写一个像roll,name和merk这样的新记录。我们每次使用BinaryReader都可以读取新行。

现在我必须使用BinaryReader类从该文件中搜索基于roll的任何记录。如何在文本框中输入卷并从文件中搜索记录。



谢谢。


Can we write a new record like roll, name and merk in new line every time using BinaryWriter. Can we read new line every time using BinaryReader.
Now I have to search any record based on roll from that file using BinaryReader class . How can i enter a roll in textbox and search the record from file.

Thank you.

推荐答案

使用那些数据,可能你不能 - 这是一种非常糟糕的数据格式,因为你不知道你写的字符串有多长 - 而你只能通过获取文件大小并减去所有数据的长度来判断。你添加的其他零碎。而你只能通过首先找出字符串的长度和读取或寻找它来读取最后的整数。



你的文字暗示这将是一个记录很多,所以你将无法轻易找到记录开始和结束的地方!



我建议你想看看你是怎么做的在继续之前要更紧密一点,并设计一种更适合搜索的数据格式。



这是家庭作业吗?如果没有,你可以采取一些措施使其变得更容易(比如没有使用BinaryWriters ...)
Using that data, probably you can't - it's a very poor data format in that you don't know how long the string you are writing is - and you can only tell by taking the file size and subtracting the lengths of all the other bits and pieces you have added. And you can only read the final integer by first finding out how long the string is and reading or Seeking past it.

And your text implies that this will be one record of many, so you will not be able to easily work out where records begin and end at all!

I would suggest that you want to look at how you do this a bit more closely before you continue, and design a data format that will be more amenable to the searching.

Is this homework? If not there are things you can do to make it easier (like not faffing about with BinaryWriters...)


搜索使用 BinaryReader 你可以在检查某些条件的同时按顺序读取元素。



以下示例显示将两个项目附加到文件然后尝试使用字符串值再次找到其中一个作为条件:



To search a file written using BinaryReader you can sequentially read the elements in the same order they were written whilst checking some condition.

The below example show appending two items to a file and then trying to find one of them again using the string value as a condition:

using System;
using System.IO;

namespace Stuff {

    class Program {

        private static void Append(int a, string b, int c) {
            using (var fs = new FileStream(@"C:\Temp\data.dat", FileMode.Append)) {
                using (var br = new BinaryWriter(fs)) {
                    br.Write(a);
                    br.Write(b);
                    br.Write(c);
                }
            }
        }

        private static bool TryFindString(string s, out int a, out int c) {
            a = 0;
            c = 0;
            using (var fs = new FileStream(@"C:\Temp\data.dat", FileMode.Open)) {
                using (var br = new BinaryReader(fs)) {
                    while (fs.CanRead) {
                        // It is important to read all the values here in the order
                        // they were written, not just the search item.
                        a = br.ReadInt32();
                        var b = br.ReadString();
                        c = br.ReadInt32();

                        if (s == b)
                            return true;
                    }
                }
            }
            return false;
        }

        static void Main(string[] args) {
            Append(1, "A", 100);
            Append(2, "B", 200);

            int a, c;
            if (TryFindString("B", out a, out c)) {
                Console.WriteLine("Found string {0}, a={1} and b={2}", "B", a, c);
            }
        }
    }
}





希望这有帮助,

Fredrik



Hope this helps,
Fredrik


这篇关于使用BinaryWriter和BinaryReader操作搜索c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆