Solved

Segmentation fault on char array bit wise operation

Posted on 2006-10-22
2
285 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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

776 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