Solved

Segmentation fault on char array bit wise operation

Posted on 2006-10-22
2
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use conditional statements in the C programming language.

691 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