LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-30-2005, 06:52 AM   #1
FarAway
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
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?
 
Old 03-30-2005, 07:24 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
In the code you posted the buf doesn't point to anything thus the result of strlen(buf) is undefined.
 
Old 03-30-2005, 07:28 AM   #3
FarAway
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Original Poster
Rep: Reputation: 0
sorry i forgot to put in the code buf = "ff"...
but i still got seg fault...

the only way is i mmap the whole file and thus mf point to start of the file.

but if the file is 16MB, is it ok to do it this way?
 
Old 03-30-2005, 07:38 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
In the code you posted (always post a complete and tested program copied and pasted and not just type something up which you think is similar to the actual code) you don't check if mmap() fails. You should do that and print the error. mmap() doesn't like offsets that are too large (not likely) or offsets that doesn't align with pagesize boundaries (likely).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Dynamic MMap ran out of room" error when adding new apt source list hasanito Debian 25 05-27-2013 03:56 AM
mmap function professional1983 Programming 1 06-14-2005 04:23 AM
using mmap AngryLlama Programming 1 02-09-2005 08:53 AM
kiobuf vs. mmap j4r0d Programming 8 01-10-2005 03:18 AM
xcdroast - cdrecord.mmap: Input/output error. write_g1: scsi sendcmd: cmd timeout masgari Linux - Software 11 03-17-2004 10:55 PM


All times are GMT -5. The time now is 11:28 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration