?
Solved

Segmentation fault on char array bit wise operation

Posted on 2006-10-22
2
Medium Priority
?
317 Views
Last Modified: 2010-04-15
myFile.txt is a binary file:

Followed is the result of od -Ax -tx1 myFile.txt:
000000 68 01 00 00 a0 05 00 00 48 00 00 00 38 05 00 00

Followed is the complete code

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>//O_RDONLY


int main(int argc, char *argv[])
{
     if (argc!=3){
          printf("Please enter exactly three arguments.\n");
     }else{
                 //myFile.txt is being memory map
          int f=open(myFile.txt,O_RDONLY);
          int n=lseek(f,0,SEEK_END);
          char * mf=mmap(0,n,PROT_READ,MAP_PRIVATE,f,0);
          int i;
          for (i=0;i<16;i++){
               mf[i]&=0x0F; //<--this gives me segmentation fault, what is the reason and how to correct it?
          }
         
     }

   return 0;
}



0
Comment
Question by:william007
2 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 17783433
>mf[i]&=0x0F;
Assigns it back to mf[i] which has been mmaped using PROT_READ
0
 
LVL 9

Author Comment

by:william007
ID: 17794375
Thanks=)
0

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

864 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