Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-10-22
6
Medium Priority
?
242 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

885 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