Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

About mmap and lseek

Posted on 2006-10-22
5
Medium Priority
?
746 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 2000 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 2000 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

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.

Question has a verified solution.

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

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

715 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