473,836 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clearing read stream buffers

JG
Does anyone know a standard (or supported on Linux, Mac, Win32) way to
clear a read stream buffer (standard ANSI C file stream)?

I would even settle for a platform specific way of doing it.

And no, I know I can use direct low level I/O or non-buffered to do
reads, but for my app, I need the buffering. I can implement myself,
but this is not optimal.

Example, I open a read only file using fopen(). I periodically know
from external methods, that the underlying file has been updated.
Before I do my next read on the stream, I want to flush the read
buffers and force it to read from disk (or kernel cache).

fflush seems to work on Linux, but not sure it is standard.

I don't see any function to do this, but it seems very needed.

I can also trigger a flush of the buffers by fseeking to a much
different area of the file. Does any fseek, even offseting by 1 index
always force a flusshing of the buffers?

Nov 14 '05
18 5708


jschultz wrote:
Ok, so it seems the only portable and guaranteed way to force a C input
stream to re-read from disk is to close/re-open/re-seek the file?
Doesn't that seem like a lot of overkill for a simple/common idea?
The C Standard tries to stear clear of describing how
multiple programs can run concurrently in the same environment,
of how they can synchronize their operations, and of what
effects one program can have on another. This is a Good Thing,
because a programming language standard that tried to address
such matters would limit its applicability rather severely.
If the Standard adopted in 1989 had embodied the Windows 3.1
process model, for example, how useful would that Standard be
for Unix, VMS, OS/400, MVS -- or even current Windows versions?

In short, the idea is less "simple" than it may appear to
someone who's struggling with one particular issue. (And in
my three decades of writing C I can't recall needing to do
what you describe, so "common" may be hard to defend, too.)

.... and in a follow-up: I mean performance overkill specifically. Because our program needs to
do these kinds of reads very quickly and opening/closing/seeking and
THEN reading seems like a ton more work than it should be.


When you measured the speed of fclose()/fopen()/fseek(),
how slow did you find it to be? ("It is a capital offense
to theorize before one has data." -- S. Holmes)

--
Er*********@sun .com

Nov 14 '05 #11
On 11 Feb 2005 08:28:27 -0800, JG
<ja*********@gm ail.com> wrote:
Example, I open a read only file using fopen(). I periodically know
from external methods, that the underlying file has been updated.
Before I do my next read on the stream, I want to flush the read
buffers and force it to read from disk (or kernel cache).

fflush seems to work on Linux, but not sure it is standard.
It isn't, fflush is only specified on output.
I don't see any function to do this, but it seems very needed.
fseek to the end? Or, if you want to read the newly added data, fseek
to the last read position (possibly with an fseek to the start first).
I can also trigger a flush of the buffers by fseeking to a much
different area of the file. Does any fseek, even offseting by 1 index
always force a flusshing of the buffers?


It's implementation dependent, the C standard doesn't say (or know)
anything about underlying buffers. At least one implementation I've
used would treat a backwards seek within a buffer as just adjusting a
pointer. The only thing which is guaranteed to work is closing the file
and opening it again (and seeking to the last read position in your
case).

However, I'd guess that doing fseek to the end of the file should always
cause any /new/ data to be read in (not necessarily anything changed in
data already read), so if the external changes are only appending to the
file I'd guess that it should work. But really it's an implementation
question, I don't think thre's any portable solution apart from closing
and opening the file each time.

Chris C
Nov 14 '05 #12
On 11 Feb 2005 11:20:38 -0800, in comp.lang.c , "jschultz"
<js******@d-fusion.net> wrote:
I mean performance overkill specifically. Because our program needs to
do these kinds of reads very quickly and opening/closing/seeking and
THEN reading seems like a ton more work than it should be.


Which is why specific implementations may nor may not offer a facilityto do
this more efficiently, but in a nonstandard way. You really need to ask
experts in your platform, not generalists in C.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >
Nov 14 '05 #13
I was just asking if there was a standard way to force a C input stream
to forget what's in its buffers without destroying the stream itself.
Apparently there isn't. That seems like a rather silly hole to me as
it would be VERY simple for a stream to forget what it has already read
in and then re-read on the next read call.

Nov 14 '05 #14
In article <11************ **********@f14g 2000cwb.googleg roups.com>
jschultz <js******@d-fusion.net> wrote:
Ok, so it seems the only portable and guaranteed way to force a C input
stream to re-read from disk is to close/re-open/re-seek the file?
Pretty much, yes.
Doesn't that seem like a lot of overkill for a simple/common idea?


It is actually quite uncommon, and sometimes not all that simple.
In particular, you first have to discover that the file has changed,
and that will take code that lies outside the purview of the C
standards (at which point you can perhaps abandon stdio entirely
and simply use open/read/lseek/close, if those are the underlying
primitives).

I included an fpurge() function in the 4.xBSD stdio, but it does
not seem to have caught on. The specification for fpurge() is,
roughly speaking, "forget all buffered data recorded in this stdio
`FILE *' thing". An fpurge() followed by an fseek() to the desired
location would do the trick (although I am not 100% sure that my
seek-optimization code would actually work right with the purge in
this case :-) ).

Note that the 4.xBSD fpurge() also works on output files, discarding
as-yet-unwritten-to-the-underlying-system data.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #15
In article <87************ @benpfaff.org>, bl*@cs.stanford .edu says...
"jschultz" <js******@d-fusion.net> writes:
One way I was thinking of doing this is:

if (setvbuf(read_s tream, NULL, _IONBF, 0) != 0 ||
setvbuf(read_st ream, NULL, _IOFBF, 0) != 0) {
return -1;
}
From C99:

The setvbuf function may be used only after the stream
pointed to by stream has been associated with an open file
and before any other operation (other than an unsuccessful

^^^^^^^^^^^^^^^ ^^^^^^^^^^^ call to setvbuf) is performed on the stream. ^^^^
Thus, calling setvbuf() twice is always undefined, regardless of
when you do it.


Unless it fails the first time and you retry? I don't see this
as very likely, but ...

--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #16
On 11 Feb 2005 21:32:06 -0800, jschultz
<js******@d-fusion.net> wrote:
I was just asking if there was a standard way to force a C input stream
to forget what's in its buffers without destroying the stream itself.
Apparently there isn't. That seems like a rather silly hole to me as
it would be VERY simple for a stream to forget what it has already read
in and then re-read on the next read call.


Not if the cards have already fallen into the hopper the first time they
were read! Not everything is a disk file...

Chris C
Nov 14 '05 #17
jschultz wrote:
I was just asking if there was a standard way to force a C input stream
to forget what's in its buffers without destroying the stream itself.
Apparently there isn't. That seems like a rather silly hole to me as
it would be VERY simple for a stream to forget what it has already read
in and then re-read on the next read call.


"Very simple?" Let's see you do it when reading
from the keyboard. Or from a socket, or from a sensor
connected to a serial port, or from /dev/random, or ...

You've been given a solution that works for streams
attached to reopenable data sources (seekability is helpful,
but not essential). Use it, and stop whining!

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #18
I imagine on a non-random seek stream (socket, serial port, terminal,
/dev/random, etc.) that such a function would discard whatever it has
already read in -- say, for example, if you are using line buffering
instead of block buffering this could be useful.

To make file streams consistent with "real" streams, the file position
should probably be advanced by how ever many bytes were discarded and
on such streams could be ascertained with a call to ftell or fgetpos.

Anyway, immature and snide remarks aside, it would very easy to do this
and would prevent suprising results from pedagoguish code like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define RECORD_SIZE 100

int main(int argc, char **argv)
{
FILE * writer;
FILE * reader;
char write_buf[RECORD_SIZE];
char read_buf[RECORD_SIZE];
int i;

srand(time(NULL ));

if (argc != 2) {
exit(fprintf(st derr, "Usage: %s <file_out>\r\n" , argv[0]));
}

if ((writer = fopen(argv[1], "r+")) == NULL) {
exit(fprintf(st derr, "fopen '%s' w/ r+ failed!\r\n", argv[1]));
}

if ((reader = fopen(argv[1], "r")) == NULL) {
exit(fprintf(st derr, "fopen '%s' w/ r failed!\r\n", argv[1]));
}

while (1) {

for (i = 0; i < RECORD_SIZE; ++i) {
write_buf[i] = (char) rand();
}

if (fwrite(write_b uf, RECORD_SIZE, 1, writer) != 1 ||
fflush(writer) != 0) {
exit(fprintf(st derr, "fwrite/fflush failed!\r\n"));
}

/* for random seek media -- insert fpurge/fseek call here */

if (fread(read_buf , RECORD_SIZE, 1, reader) != 1) {
exit(fprintf(st derr, "fread failed!\r\n"));
}

if (memcmp(read_bu f, write_buf, RECORD_SIZE) != 0) {
abort();
}
}

return 0;
}

The output file must exist before running. If the output file <=
RECORD_SIZE bytes long then the program will run. Otherwise it will
immediately fail due to cache incoherence caused by the C library's
read ahead caching of "junk" data from the file's previous contents.

That would be fine, IF the C standard offerred a way around this
problem without the expensive operation of tearing down and rebuilding
the stream. The fact that it doesn't is suprising to me.

Nov 14 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2292
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am supposed to change the buffer size and see how this affects the read time. In other words, the buffer is supposed to limit how much of the file gets read per call, and cause some change in speed. I am supposed to do the same with fread as well, but...
6
31081
by: Carol Pedder | last post by:
Hello all, may I ask a question? Having just started C++ (using microsoft visual studio), I am using cin/cout for console applicarions (yes I know it doesn't happen in the real world!) and I'm finding it difficult to track down a method of clearing the input buffer after a cin (to get rid of potential rubbish). cin.flush() has been mentioned, but isn't an option on visual studio and cin.ignore() requires you to specify number of chars...
3
4323
by: Rakesh Sinha | last post by:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not precisely what I was looking for. The problem is with respect to reading 'long double's from a stream separated by comma. #include <iostream>
4
7499
by: news.eircom.net | last post by:
Hello, How do you clear the contents of an ostringstream object? I've included some example code so you can see what I mean. t.i.a. Craig H. #include <iostream>
5
17557
by: Sandy | last post by:
Hi, In one of my interview i was asked the difference between Stream and Buffer. can anybody explain the difference. Thanks
9
4016
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int main(void) { char buf = {0};
0
2993
by: PD | last post by:
I am trying to stream out a PDF file via the response object and when I execute the code it always brings up the first document that I pulled up.Even when I step through the code, I can see the new values in theData string array (see code below), but it serves up the first document that I had run this code with. I streamed theData into a text file to look at the values and it contained the appropriate ones for the new document. Even when I...
2
4895
by: microdevsolutions | last post by:
Hello I've seen examples to read a file from somewhere into a HttpWebRequest object then write it to a HttpWebResponse object then stream it into a Stream object, very similar to the following code snipet :- ============================================== Uri uri = new Uri("http://www.somewhere/pdf/image1.pdf"); HttpWebRequest httpRequest = null;
4
2339
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is compressed into another stream (output). I then copy the stream (output) to another stream (input2). The compressed stream (input2) is then decompressed into a final stream (output2). My question is this. I create input2 from output.GetBuffer()...
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10584
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10248
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5815
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3108
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.