?
Solved

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

Posted on 2006-10-22
6
Medium Priority
?
234 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

770 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