Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have some data like roll, name and mark. I have written these to a file. Like
C#
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();

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.
Posted
Updated 29-Oct-13 23:28pm
v5

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...)
 
Share this answer
 
v2
Comments
[no name] 30-Oct-13 6:43am    
Can you give me one answer.
If i use FileMode.Append with br.Write("\r\n"); it let's you append new record in new line. can we start reading file from new line. Suppose start from first line read roll, if it matches read rest(name and mark) and break, if does not matche, start reading from new line. I just want these and nothing else.

--Thank u
OriginalGriff 30-Oct-13 7:02am    
No.
The simple reason is that binary files do not *have* newlines. It is perfectly feasible that your integer values could contain "\r\n" codes - which would f*ck you up completely.
You need to look at doing this is a more sensible way: fixed length records is a good idea, or if not then storing the data in such a way that you can get at records directly.
One of the things you can do with a Binary file is read "a line" - simply because the data may replicate it.

What are you trying to achieve with this? There may be a simpler solution.
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:

C#
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);
            }
        }
    }
}


Hope this helps,
Fredrik
 
Share this answer
 
Comments
[no name] 30-Oct-13 5:25am    
Thank u, Fredrik, but this solution already i have. I am looking for using br.BaseStream.Seek(); br.BaseStream.Position;
Fredrik Bornander 30-Oct-13 5:31am    
You can't with that data format. Unless you know exactly how wide each field or group of fields are, then you can't seek to the next position.

In your case, you're using a string and that's going to take up a variable amount of space so without reading each element, there's no way of knowing.

Either re-think your data format or use sequential reads.
[no name] 30-Oct-13 6:03am    
If i use FileMode.Append and br.Write("\r\n"); it let's you append new record in new line. can we start reading file from new line. Suppose start from first line read roll, if it matches read rest and break, if does not matche, start reading from new line. I just want these and nothing else.

--Thank u
Fredrik Bornander 30-Oct-13 7:40am    
How would you find the line break?
Using a binary writer what's the difference in writing a '\n' and byte value of 10?

If you want to read line based, then you're not looking to use Postion and Seek(), I think.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900