473,804 Members | 2,755 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 #1
18 5706

JG wrote:
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.


once you've determined that there is still a bunch of characters
sitting in the input stream (should include '\n' at least), then you
can use:

while( (c = fgetc(FOOFILE) != '\n' )
;

Nov 14 '05 #2
Luke Wu wrote:
once you've determined that there is still a bunch of characters
sitting in the input stream (should include '\n' at least), then you
can use:

while( (c = fgetc(FOOFILE) != '\n' )
;


He is not asking the typical "How do I forget whatever is in stdin
question?"

He is asking with an actual file on disk (not a pipe/terminal/etc) that
he performs a read upon, how can he portably and efficiently FORCE an
open read stream to re-read data he wants from disk. The problem being
that a write from another thread/stream/fd/process has changed the
underlying file and he wants to ensure that the input stream doesn't
just re-give him what is in its buffers from an earlier read.

Nov 14 '05 #3
On Fri, 11 Feb 2005 08:28:27 -0800, JG wrote:
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).
The only sure way I can think of is to close and reopen the stream, using,
say, freopen().
fflush seems to work on Linux, but not sure it is standard.
It isn't. In standard C applying fflush() to an input stream invokes
undefined behaviour.
I don't see any function to do this, but it seems very needed.
I know what you mean, although surprisingly I don't remember needing it.
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?


I hope not, that would be inefficient if the data at the new position was
already in the buffer. This approach seems to be your best bet however
short of closing and reopening the file. Maybe a double seek, one to some
far off position and then to the position you want. Even that isn't 100%
guaranteed though.

Lawrence

Nov 14 '05 #4
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;
}

/* do read */

Any comments from the peanut gallery about how advisable / guaranteed
this is to work?

Nov 14 '05 #5
Luke Wu wrote:

JG wrote:
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.


once you've determined that there is still a bunch of characters
sitting in the input stream (should include '\n' at least), then you
can use:

while( (c = fgetc(FOOFILE) != '\n' )
;


You missed a ), and forgot to check for EOF.

while( (c = fgetc(FOOFILE)) != '\n' && c != EOF)
;
Nov 14 '05 #6
"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. But you're only allowed to do it at all just
after the file is opened.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Nov 14 '05 #7
On 11 Feb 2005 09:01:46 -0800, "jschultz" <js******@d-fusion.net>
wrote:
Luke Wu wrote:
once you've determined that there is still a bunch of characters
sitting in the input stream (should include '\n' at least), then you
can use:

while( (c = fgetc(FOOFILE) != '\n' )
;


He is not asking the typical "How do I forget whatever is in stdin
question?"

He is asking with an actual file on disk (not a pipe/terminal/etc) that
he performs a read upon, how can he portably and efficiently FORCE an
open read stream to re-read data he wants from disk. The problem being
that a write from another thread/stream/fd/process has changed the
underlying file and he wants to ensure that the input stream doesn't
just re-give him what is in its buffers from an earlier read.


fclose(), fopen()?

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #8
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?

Nov 14 '05 #9
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.

Nov 14 '05 #10

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

Similar topics

10
2290
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
31079
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
4322
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
7493
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
17554
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
4013
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
2991
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
2338
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
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10311
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,...
1
7613
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
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
3
2988
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.