Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

regarding lseek and fread


Hi,

       I am learing Unix internals. I have come across a problem where
i am not able to understand what is happening. As i gone through the
book i found that lseek will give the physical descriptor and ftell
will give about logical descriptor, but when i read the text using
fread the ftell and lseek should give diffrent values. but when new
process comes into to existence  ftell should use lseek value. but is
not happening the output line given below  and the code is given
below.

#include<stdio.h>
#include <unistd.h>
main()
{
   FILE *fp;
   char buf[10];
   int pid , dip;

   fp = fopen("vikas", "r");
   pid = fork();
   if (pid == 0) {
       printf("in child before read fseek is %d , lseek is %d \n",
ftell(fp), lseek(fp->_file ,0, 1));
       fread(buf, sizeof buf , 1, fp);
       buf[10] ='\0';
       printf("after child read file pointer ftell is %d, lseek is %d
%s\n", ftell(fp), lseek(fp->_file ,0, 1)
, buf);
       sleep(5);
       fread(buf, sizeof buf, 1, fp);
       buf[10] = '\0';
       printf("after child 2nd time read file pointer is %d , lseek is
%d %s\n", ftell(fp), lseek(fp->_file, 0
, 1), buf);
    }
    else {
        wait(0);
        printf("intially in parent file ponte %d, lseek is %d %s\n",
ftell(fp), lseek(fp->_file, 0, 1), buf);
        fread(buf, sizeof buf, 1, fp);
        buf[10] ='\0';
        printf("after parent read file pointer is %d ,lseek is %d %s
\n", ftell(fp), lseek(fp->_file, 0, 1), bu
f);
    }

}

output is
in child before read fseek is 0 , lseek is 0
after child read file pointer ftell is 10, lseek is 4096 AAAAAAAAAA
after child 2nd time read file pointer is 20 , lseek is 4096
AAAAAAAAAA
intially in parent file ponte 20, lseek is 20
after parent read file pointer is 30 ,lseek is 4096 AAAAAAAAAA

i am not able to undersand the parent portion. why the ftell is using
20  as for a new process it should use lseek value, and how come the
lseek value has been changed?.

Appreciate your help in this read.

Thanks,
Venkat.

In article <1180242747.733365.206@k79g2000hse.googlegroups.com>,

venkat  <venkatavi@gmail.com> wrote:
>       I am learing Unix internals. I have come across a problem where
>i am not able to understand what is happening. As i gone through the
>book i found that lseek will give the physical descriptor and ftell
>will give about logical descriptor,

lseek() and fork() and unistd.h are not defined by the C language.
You should ask your question in a newsgroup that deals with your
OS internals.

>       printf("in child before read fseek is %d , lseek is %d \n",
>ftell(fp), lseek(fp->_file ,0, 1));

<OT>
lseek is defined as returning off_t which might well not be
an int (and usually is not), so using a %d format for the
output of the result of lseek() is not correct.

By the way, for portability you should not be assuming the
existance of a _file member of the FILE structure. That's
an implementation aspect that is subject to change. You should
use the fileno() macro.

But for the rest of your question, after making the above fixes,
you need to consult a unix newsgroup.
--
  There are some ideas so wrong that only a very intelligent person
  could believe in them.                              -- George Orwell

venkat wrote:
> Hi,

>        I am learing Unix internals. I have come across a problem where
> i am not able to understand what is happening. As i gone through the
> book i found that lseek will give the physical descriptor and ftell
> will give about logical descriptor, but when i read the text using
> fread the ftell and lseek should give diffrent values. but when new
> process comes into to existence ftell should use lseek value.

You have aseveral simple errors as far as the C programming language is
concerned.  ftell() returns a long and you incorrectly try to print that
value with the designator "%d".  In addition, you attempt illegally to
        buf[10] ='\0';
but there is no such buf[10].  This should cause in segfault in most
varieties of Unix.  Further, you make no effor to check that "vikas" was
successfully opened for read.  You combine the incompatible errors of
using an implicit int return type for main (allowed only before C99) and
omitting the explicit return (or exit()) from main (allowed only in C99
and later).

As far as this newsgroup is concerned, the difference between lseek and
ftell is simple: there is a standard C function called ftell, there is
not such a standard C function called lseek.  Since C I/O identifies
streams through pointers-to-FILE and the most common definition of lseek
(POSIX, but in the programmer's namespace in C, so it could be anything)
identifies the stream with an integer "file descriptor", you have a big
hint that lseek is not a standard C function.  The fact that you need to
include the non-standard <unistd.h> header is another big hint.
Additionally, form(), wait(), and sleep() are not standard C functions.

What this means is that you need to ask your question in a newsgroup
where lseek is topical.  That means one concerned with POSIX or some
near-relative of Unix.

venkat wrote:

> I am learing Unix internals. I have come across a problem where
> i am not able to understand what is happening. As i gone through
> the book i found that lseek will give the physical descriptor and
> ftell will give about logical descriptor, but when i read the text
> using fread the ftell and lseek should give diffrent values. but
> when new process comes into to existence  ftell should use lseek
> value. but is not happening the output line given below  and the
> code is given below.

Unix is off-topic here.  Try comp.unix.programmer.  This group
deals with the standardized C language.

--
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
 <http://www.securityfocus.com/columnists/423>
 <http://www.aaxnet.com/editor/edit043.html>
 <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc