lseek problem

jsmng at csuna.cs.uh.edu jsmng at csuna.cs.uh.edu
Tue Oct 10 10:17:50 AEST 1989


I posted a problem I encountered  with lseek mainly due to my own
defiency in C. The problem was I was aware of the difference in
buffered I/O and low lever I/O. I have received a lot of constructive
comments. They are all similar so I chose one which seems to be quite
informative to post. Thanks for the contribution.


>From amdcad!cayman!tim at ames.arc.nasa.gov Mon Oct  9 09:08:10 1989
Return-Path: <amdcad!cayman!tim at ames.arc.nasa.gov@cs.uh.edu>
Received: from ames.arc.nasa.gov by sun1.cs.uh.edu (4.0/SMI-3.2)
	id AA05545; Mon, 9 Oct 89 09:08:07 CDT
Received: by ames.arc.nasa.gov (5.61/1.2); Mon, 9 Oct 89 07:06:51 -0700
Received: by amdcad.AMD.COM (5.51/2.6)
	 id AA13558; Mon, 9 Oct 89 06:56:33 PDT
Received: by cayman.AMD.COM (4.0/SMI-4.0)
	id AA13703; Mon, 9 Oct 89 08:59:11 CDT
Date: Mon, 9 Oct 89 08:59:11 CDT
From: amdcad!cayman!tim at ames.arc.nasa.gov (Tim Olson)
Message-Id: <8910091359.AA13703 at cayman.AMD.COM>
To: jsmng at sysmgr.cs.uh.edu
Subject: Re: lseek problem
Newsgroups: comp.lang.c
In-Reply-To: <14657 at uhnix1.uh.edu>
Organization: Advanced Micro Devices, Austin, TX
Cc: 
Status: RO

In article <14657 at uhnix1.uh.edu> you write:
| I have a small problem about lseek. I
| tried to use the following program to
| lseek a file ( with numbers 1 2 3 ).
| I expect to get back 1, blank, and 1 again.
| But my output was 1, blank and 2.
| Initially I use fscanf(fp,"%f",&float_variable)
| in place of fgetc and received 1 2 3 instead
| of 1 2 1. Can anyone tell me what I did wrong?
| Thanks. I think this is probably something
| simple so please just send me a mail and
| I will summarize it.

You can't mix lseek and f{scan,get}f.  The later functions use the
buffered I/O provided in the stdio package.  What is happening is that
on your first fscanf, an attempt is made to read BUFSIZE bytes from
the file and place them into the _iob structure.  Once this is done,
the fscanf reads out of this buffer.  Lseeks do not affect this buffer
at all; they change the actual file pointer (affecting reads and
writes).  What you want to use is fseek, which works with the other
"f" functions.

-- 
	-- Tim Olson
	Advanced Micro Devices
	(tim at amd.com)



More information about the Comp.lang.c mailing list