Solved

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

Posted on 2006-10-22
6
209 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

777 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