Link to home
Start Free TrialLog in
Avatar of william007
william007

asked on

Segmentation fault on char array bit wise operation

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;
}



ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of william007
william007

ASKER

Thanks=)