Solved

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

Posted on 2006-10-22
6
193 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Goodbye, so long 10 92
how to understand recursion 12 206
Arduino EDI - Programming language 3 64
Acrinis True image 2 21
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…
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 how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

746 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

13 Experts available now in Live!

Get 1:1 Help Now