Solved

About mmap and lseek

Posted on 2006-10-22
5
665 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
[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
5 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
ID: 17782983
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
ID: 17783003
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
ID: 17783007
EOF is also part of file .. else you would never know where the file ends!!!
0
 
LVL 9

Author Comment

by:william007
ID: 17783049
I see:)
0
 
LVL 9

Author Comment

by:william007
ID: 17794381
Thanks=)
0

Featured Post

Technology Partners: 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

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 tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

739 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