Solved

Null on non-zero entry but not null on zero entry in binary file

Posted on 2006-10-22
6
223 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++){
                  if (mf[i]=='\0'){
                      printf("File content= %p\n",mf[i]);
                  }else{
                        printf("Nothing\n");
                  }
            }
            
      }

   return 0;
}
Result of execution
===============
999
999
File content= (nil)
File content= (nil)
999
999
File content= (nil)
File content= (nil)
999
File content= (nil)
File content= (nil)
File content= (nil)
999
999
File content= (nil)
File content= (nil)

My Question is
I suppose it is mf[i]!='\0' on non-zero entry, and mf[i]=='\0' on zero entry, but the result is
mf[i]=='\0' on non-zero entry, and mf[i]!='\0' on zero entry, what is the reason?

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
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:0xC0DEB07
ID: 17783348
There is difference between '\0' and '0' ;)
0
 
LVL 9

Author Comment

by:william007
ID: 17783365
Can you explain what happening on for getting that result?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17783422
I dont see a single print of "nothing" in the output!!
Also the code would not compile since filename is not in quotes ... Is this really the exact code and output?
0
Technology Partners: 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!

 
LVL 9

Author Comment

by:william007
ID: 17783436
Sorry, correction as followed
1. Put the filename in the quote

2. Result
=====
Nothing
Nothing
File content= (nil)
File content= (nil)
Nothing
Nothing
File content= (nil)
File content= (nil)
Nothing
File content= (nil)
File content= (nil)
File content= (nil)
Nothing
Nothing
File content= (nil)
File content= (nil)
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17783439
This output is consistent with the code

               if (mf[i]=='\0'){
                   printf("File content= %p\n",mf[i]);
               }else{
                    printf("Nothing\n");
               }

you print value only if == '\0' ... '\0' is same as 0
0
 
LVL 9

Author Comment

by:william007
ID: 17794378
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

Suggested Solutions

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…
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…
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 for-loops in the C programming language.

730 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