Thread: mmap() error

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    mmap() error

    I have a file "b.txt", which is 16 bytes long:

    b.txt:
    00 12 32 a5 6f ff 11 67 00 00 00 11 fa 90 44 66

    I use the following code to change the first 2 bytes of b.txt, and it works.

    Code:
    int fd, i;
    char *mf;
    char *buf ;
    
    fd = open("b.txt", O_RDWR);
    
    mf = mmap(0, 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    
    for(i = 0; i < strlen(buf); i++)
        mf[i] = buf[i];
    
    msync(mf, 2, MS_SYNC);
    however, if i use

    Code:
    mf = mmap(0, 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 2);
    to change the 3rd and 4th (i.e. 32 and a5) of b.txt, segmentation fault occurs.

    Can anyone tell me how to map the middle part of a file into memory and do changes to it?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well do you get any errors when you mmap?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you not read my reply to your last post:
    http://cboard.cprogramming.com/showthread.php?t=63574
    .. And double check you're using the correct value as the last parameter on mmap().
    http://www.ecst.csuchico.edu/~beej/guide/ipc/mmap.html
    To save your lazy ass from following the link , here's an extract from the page I refer to:
    void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);

    off
    This is the offset in the file that you want to start mapping from. A restriction: this must be a multiple of the virtual memory page size. This page size can be obtained with a call to getpagesize().
    If you look at that page, you'll find sample code showing you how to access bytes at a specific offset.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    Thanks...

    I fixed the problem already..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM