Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
actually i want read a file from memory and save that file in the increasing order of the names contineously ,i am able to write only one file so ......

What I have tried:

using (FileStream stream = File.OpenRead("C:\\veer1.raw"))
                       using (FileStream writeStream = File.OpenWrite("D:\\file2.raw"))
                       {
                           BinaryReader reader = new BinaryReader(stream);
                           BinaryWriter writer = new BinaryWriter(writeStream);

                           // create a buffer to hold the bytes
                           byte[] buffer = new Byte[1024];
                           int bytesRead;

                           // while the read method returns bytes
                           // keep writing them to the output stream
                           while ((bytesRead =
                                   stream.Read(buffer, 0, 1024)) > 0)
                           {
                               writeStream.Write(buffer, 0, bytesRead);
                           }
Posted
Updated 14-May-17 21:16pm
v3
Comments
Maciej Los 15-May-17 2:46am    
If you want to write data in "increasing order of the names", you have to sort them first! I do not see corresponding code...
Mehedi Shams 15-May-17 2:47am    
Hi Member 13142768,

What are the contents of the file? Names or numbers? I mean, how do you want to compare? I don't see any comparison attempt in the code - it is just reading from one file an flushing to the other; the files would be the same.
Veerendra-13142768 15-May-17 2:57am    
yup i want copy the same content
Veerendra-13142768 15-May-17 2:49am    
i am a fresher so how to sort i am not getting can you help me...
Richard MacCutchan 15-May-17 3:10am    
Where is the data that needs to be sorted in each record?

1 solution

Hi Member 13142768,

You are reading and writing straight-away. You need to store the items that you read in memory first. For this you can use a generic list. The following lines may change:
while ((bytesRead =
        stream.Read(buffer, 0, 1024)) > 0)
{
    writeStream.Write(buffer, 0, bytesRead);
}
To:
List<byte[]> StrList = new List<byte[]>();
                
while ((bytesRead =
        stream.Read(buffer, 0, 1024)) > 0)
{
    StrList.Add(buffer);
}
That is, you are reading the entire binary contents in a list as byte array entities. But I am still not sure about the contents of your file as you forgot to answer that.

After sorting you need to write the contents of the list.

The built-in sort method of list will not work here as it has binary byte array. You will need IComparer interface to assist. Here is a good solution to such sorting:

c# - Sorting list of list of bytes or list of byte arrays - Stack Overflow[^]
 
Share this answer
 

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