Solved

Segmentation fault on char array bit wise operation

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to align numbers in C using the %d 2 89
C Language combined operators 28 106
xamarin c# deserialize Json containing nested object 2 81
smtp c source code 7 46
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now