5

I am working on memory mapped files. Is there any way to know the length of memory mapped file content? What I want is to append the existing memory mapped file. Its easy to append the bytes in the file but I am looking to append string.

We can check the CAPACITY property, but it returns the bytes size I think.

To be more clear, I am explaining the scenario. I am creating Memory mapped file A. I write "Hello" when I create it. It works fine. Now I want to write the "World" into existing file A.

I am using below code for this:

 var file = MemoryMappedFile.OpenExisting("myFile");
            string str = "String to append";
            using (var view = file.CreateViewAccessor())
            {
                using (var stream = file.CreateViewStream())
                {
                    System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
                    writer.Write(str.ToArray(), INT SIZE,Convert.ToInt32(view.Capacity), str.Length);//Error
                }
            }
            using (var stream = file.CreateViewStream())
            {
                Console.WriteLine("Reading appended File");
                System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
                Console.WriteLine(reader.ReadString());
                Console.WriteLine(string.Empty);
            }

I have marked the line with (//) that asks for the size. I want to know the value for (INT SIZE) Any help would be appreciated.

[EDIT] I am using C#, Visual Studio 2010.

Now I am using this code to append:

var file = MemoryMappedFile.OpenExisting("myFile");
            string str = "String to append";
            string str1 = string.Empty;


            using (var stream = file.CreateViewStream())
            {
                System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);

                str1 = reader.ReadString();
                str1 = str1 + "\n" + str;

                System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
                writer.Write(str1);
            }
            using (var stream = file.CreateViewStream())
            {
                Console.WriteLine("Reading appended File");
                System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
                Console.WriteLine(reader.ReadString());
                Console.WriteLine(string.Empty);
            }

But its not appending anything. Can you please check my code?

Deepak Kumar
  • 672
  • 4
  • 15
  • 31

1 Answers1

0

You can't append to a string in the stream by writing more character after it, as that won't change the length of the string already written.

Also, there is no method of the BinaryWriter that lets you write to a specific position in the stream like that. For the overload that takes a character array and two integers, the second parameter specifies the position in the array, not the position in the stream.

If you want to change the string written in the stream, you have to read the string from the stream and rewrite the stream with the new string.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks for the reply. Can you please re-check my question? I tried it what I understand from your answer. But its not working. – Deepak Kumar Mar 07 '12 at 08:07
  • @Deepak: You have to reset or reopen the stream before writing to it, otherwise you will contine writing where you left off reading so the original string is still the first string in the stream. – Guffa Mar 07 '12 at 08:11
  • Hello Guffa, I have a problem. Can you please reply here? or shall I ask on SO? – Deepak Kumar Mar 09 '12 at 08:13
  • If you can see this problem http://stackoverflow.com/questions/9631778/consume-disposed-memory-mapped-file – Deepak Kumar Mar 09 '12 at 11:53
  • Please, what is the answer of the main question: "Memory Mapped File Length" – Saw Mar 05 '13 at 16:24
  • 2
    @MohamedSakherSawan: The length of the memory mapped file is the same as the capacity. If you open a file as a memory mapped file, it's the length of the file. If you create a new memory mapped file you specify the capacity yourself. – Guffa Mar 05 '13 at 17:01
  • But when try to read a memory mapped file, it reads more than it's capacity, at minimum, it reads 4096 bytes! – Saw Mar 05 '13 at 17:05
  • @MohamedSakherSawan: I can't see a way to open a memory mapped file with a capacity less than the file size? Do you mean that you read a file using a `FileStream` that was created using a memory mapped file? – Guffa Mar 05 '13 at 17:19
  • I have created a memory mapped file, but I can't read all of it's contents, I have asked two questions, http://stackoverflow.com/questions/15229166/read-memory-mapped-file-or-knowing-its-size-to-read-it-correctely http://stackoverflow.com/questions/14953393/read-all-contents-of-memory-mapped-file-or-memory-mapped-view-accessor-without-k – Saw Mar 05 '13 at 17:24
  • @MohamedSakherSawan: Further investigation gives that the memory mapped file knows the exact capacity, but it will absolutely not tell you. The capacity of a view accessor is not limited to the capacity of the actual memory mapped file, instead it is returned in whole system pages. – Guffa Mar 05 '13 at 18:09