Write to mapped memory file

Posted on 2006-04-05
Medium Priority
Last Modified: 2010-08-05
I have the following code to create a file, and then map it into memory...I want to write to it and then read from it....

fd = open(argv[2], O_RDWR|O_CREAT, S_IRWXU);
if (fd < 0 )
    f_error("open1() error");
buffer = mmap(0, (size_t)MAX_BUF_SIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0);
if (buffer == MAP_FAILED)
    f_error("mmap1() error");

Thanks for your help.
Question by:trubiat
  • 2
  • 2
LVL 12

Expert Comment

ID: 16388847
What exactly is your question ?

Author Comment

ID: 16388885
i want to know how to write to the file which in mapped into the memory, how to read from it, and how to empty it (if I write something, then how do I make the file empty)...
LVL 12

Accepted Solution

rajeev_devin earned 375 total points
ID: 16388937

Author Comment

ID: 16388945
i gave you a B grade because you tried to help but did not solve my question....The link you sent me defines mmap()....it doesn't show how to write to a file mapped in memory, or read from it....thanks anyways

Expert Comment

ID: 16388961
when you call mmap() then the file contents are copied to memory and a character pointer is returned to that data. In your code "buffer" will receive pointer to data area. simply writting
   buffer[0] = 'a';   // will write character 'a' to the first location of file.
if you do
   strcpy(buffer,"test"); // will write the string "test" to the file
same way to read it just

char ch = buffer[0]
char temp[10]
will copy first 10 char of file to temp

how to empty it?? I am not sure but if you write EOF to the first location of file it may empty the file. I will check and confirm this.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question