Solved

About mmap and lseek

Posted on 2006-10-22
5
574 Views
Last Modified: 2008-03-04
int f=open("myFile",O_RDONLY);
int n=lseek(f,0,SEEK_END);
void * mf=mmap(0,n,PROT_READ,MAP_PRIVATE,f,0);

1)Am I right that using lseek(f,0,SEEK_END); to get the file size will give you filesize + 1, and hence it is more correct to use lseek(f,-1,SEEK_END);? Also, is this a correct way to get the filesize?
2)After getting mf, how should I read the file inside the memory map file?
eg my file has
****myFile start****
line1 abc
line2 def
****myFile end****
How to get "line2 def", ie the second line?

0
Comment
Question by:william007
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
1. No ... Why would it give you length +1?
int n=lseek(f,0,SEEK_END);
is correct

note that this would position the head to end of file ... you must reposition it to beginning of file

2. mmap returns pointer to mmaped area ... simply treat it like a buffer in memory ... here is an example which should answer both your questions

int main() {          
      void * ret;
      int n;

      int f = open ("ha.txt", O_RDONLY);

      n = lseek (f,0,SEEK_END);
      printf ("%d\n",n);

      n = lseek (f,0,SEEK_SET);
      ret = mmap (0, 1024, PROT_READ,MAP_PRIVATE, f,0);
      printf ("%s\n", ((char *)ret) + 10);
}
0
 
LVL 9

Author Comment

by:william007
Comment Utility
For question 1,
this is because
I type "abcdef" in a file, it gives me length 7,
even nothing in a file, it gives me length 1,
If it is correct, what is the reason for the size? abcdef...should be 6 bytes and nothing in a file should be 0 byte....
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 500 total points
Comment Utility
EOF is also part of file .. else you would never know where the file ends!!!
0
 
LVL 9

Author Comment

by:william007
Comment Utility
I see:)
0
 
LVL 9

Author Comment

by:william007
Comment Utility
Thanks=)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

744 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

12 Experts available now in Live!

Get 1:1 Help Now