Solved

About mmap and lseek

Posted on 2006-10-22
5
613 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
libcurl and C++ - Post JSON Data 8 1,291
Test against App 49 137
Compile VxWorks Toronado project under Visual Studio 11 193
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,720
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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

813 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

15 Experts available now in Live!

Get 1:1 Help Now