473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

No buffered I/O in the Standard Library?

I haven't looked very closely, but from what I'm seeing, it looks like there
are no buffered I/O streams in the Standard Library. There are stream
buffers, but not buffered streams. I don't have an excellent definition of
what a buffered stream is right off the top of my head, but it's something
like a cache that can hold data from the source, or for the destination
that can't be immediately processed. Say, for example, if you are
streaming to a network socket that sends data in larger chunks than your
process produces per output operation. You don't want to send an almost
empty packet every time you write to the stream, then sit around an wait
till the output stream becomes available again when the network driver
finishes its transmission. So you write to a buffer from which the network
driver reads.

Does the standard library not provide a generic buffered stream?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #1
7 2313
Steven T. Hatton wrote:
I haven't looked very closely, but from what I'm seeing, it looks like there
are no buffered I/O streams in the Standard Library. There are stream
buffers, but not buffered streams. [...]

Does the standard library not provide a generic buffered stream?


Uh... So, let me get this straight.... If a stream has a buffer, you still
do not consider it a "buffered stream"? Huh... Interesting...
Jul 23 '05 #2
> I haven't looked very closely
I don't have an excellent definition of what a buffered stream is


If you haven't looked hard for something you don't understand, how can
you tell if it's there or not?

Jul 23 '05 #3
Victor Bazarov wrote:
Steven T. Hatton wrote:
I haven't looked very closely, but from what I'm seeing, it looks like
there
are no buffered I/O streams in the Standard Library. There are stream
buffers, but not buffered streams. [...]

Does the standard library not provide a generic buffered stream?


Uh... So, let me get this straight.... If a stream has a buffer, you still
do not consider it a "buffered stream"? Huh... Interesting...


It just wasn't what I had expected. Fact of the matter is, it was far
simpler than I thought it would be. What I wanted to do was read a file in
to an in-memory buffer, and then process it as if it were being read from a
stream. I'm still not sure how to do that with binary data, but then I
haven't attempted that either. The trick was `ss << in.rdbuf();'. All the
examples I was seeing were reading one character at a time from cin, or one
line at at time from a file. I suspect that is not the best way to read
from system resources.

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <map>

using namespace std;

ostream& printPair(ostream& out, const pair<const string, string>& p) {
return out << "key=" << p.first << ", value=" << p.second << endl;
}

template<typename T>
istream& setPair(istream& in, map<const T,T>& m) {
string key;
string value;
in >> key >> value;
if(key=="" || value=="") return in;
m[key] = value;
return in;
}
int main(int argc, char* argv[]) {
if(!argc > 1) {
cerr << "rss filename: " << endl;
return -1;
}

ifstream in(argv[1]);

if(!in.is_open()) {
cerr << "Failed to open file: " << argv[1] << endl;
return -1;
}

stringstream ss;
ss << in.rdbuf();
typedef map<const string, string> Map_T;
Map_T smap;
while(setPair(ss, smap));

for(Map_T::iterator it = smap.begin();it != smap.end(); it++ )
printPair(cout, *it);
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #4
Steven T. Hatton wrote:
it looks like there
are no buffered I/O streams in the Standard Library.


No, streams are buffered by default. e.g.

ifstream f("...");
for(int i=0; i<100; ++i) {
char c;
f >> c;
}

does NOT do 100 calls to read(), it probably does 1.
--Phil.
Jul 23 '05 #5
Phil Endecott wrote:
Steven T. Hatton wrote:
it looks like there
are no buffered I/O streams in the Standard Library.


No, streams are buffered by default. e.g.

ifstream f("...");
for(int i=0; i<100; ++i) {
char c;
f >> c;
}

does NOT do 100 calls to read(), it probably does 1.


As an example, on my linux system, a trace of the system calls (strace)
shows for the above code:

open("...", O_RDONLY) = 3
fstat64(0x3, 0xbffff62c) = 0
old_mmap(NULL, 131072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40018000
read(3, "lakzsdfjlzadfkjaslzdfkjasdflzkja"..., 131072) = 614
read(3, "", 131072) = 0
close(3) = 0

That means that the implementation reads and buffers the data from files
in blocks of 128kB. The old_mmap is the allocation of the buffer.
Jul 23 '05 #6
Rolf Magnus wrote:
Phil Endecott wrote:
Steven T. Hatton wrote:
it looks like there
are no buffered I/O streams in the Standard Library.


No, streams are buffered by default. e.g.

ifstream f("...");
for(int i=0; i<100; ++i) {
char c;
f >> c;
}

does NOT do 100 calls to read(), it probably does 1.


As an example, on my linux system, a trace of the system calls (strace)
shows for the above code:

open("...", O_RDONLY) = 3
fstat64(0x3, 0xbffff62c) = 0
old_mmap(NULL, 131072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x40018000 read(3, "lakzsdfjlzadfkjaslzdfkjasdflzkja"..., 131072)
= 614
read(3, "", 131072) = 0
close(3) = 0

That means that the implementation reads and buffers the data from files
in blocks of 128kB. The old_mmap is the allocation of the buffer.


I'd have to read the manpages on the system to understand the output, but
I'll take your word for it. This behavior is typically what a person would
want. I'm fairly confident it is not specified by the Standard(though I
could well be wrong). There are times when this is not what a programmer
would want. For example using embedded systems with limited memory, or
very large AV files. I suspect the same might apply when working with
databases that hold all of their data in a single file. I am certain that
there were (significant) performance differences between using input
streams, and buffered input streams in Java when transferring data over a
network connection. I'm fairly confident the same applies to file access.

That seems to suggest that Java offers finer grained control over I/O than
does C++ at the same level of abstraction. That finer control comes at the
cost of more complexity in the API. Typically we would expect just the
opposite when conparing Java and C++. I have less than 24 hours experience
dealing with this topic in C++ (and many of them were spent sleeping) so I
have much to learn before I can make a meaningful assessment.

This, BTW, is where I found out how to push file contents from an ifstream
to a stringstream with one ">>".

http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

I believe it was Bob Tisdale who suggested Eckel's books as a good source on
I/O.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #7
Steven T. Hatton wrote:
Rolf Magnus wrote:
Phil Endecott wrote:
Steven T. Hatton wrote:
it looks like there
are no buffered I/O streams in the Standard Library.

No, streams are buffered by default. e.g.

ifstream f("...");
for(int i=0; i<100; ++i) {
char c;
f >> c;
}

does NOT do 100 calls to read(), it probably does 1.
As an example, on my linux system, a trace of the system calls (strace)
shows for the above code:

open("...", O_RDONLY) = 3
fstat64(0x3, 0xbffff62c) = 0
old_mmap(NULL, 131072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0) = 0x40018000
read(3, "lakzsdfjlzadfkjaslzdfkjasdflzkja"..., 131072) = 614
read(3, "", 131072) = 0
close(3) = 0

That means that the implementation reads and buffers the data from files
in blocks of 128kB. The old_mmap is the allocation of the buffer.


I'd have to read the manpages on the system to understand the output, but
I'll take your word for it.


I thought it mostly speaks for itself, but forgot that it might not be so
obvious if you're not so familiar with this kind of system.
Some explanation:

The first line says that the file was opened in read-only mode. The returned
value (the 3 at the end) is the file id.

The second line just retrieves some information about the file (first
parameter is the file id, the second a pointer to the struct that the
function fills). The return value of 0 means success. I'm not sure what the
stream does with that information.

The old_mmap in the 3rd line allocates 131072 bytes of memory. The returned
value is its address. This is the low-level OS interface that malloc and
new on my system use.

The next two lines do the actual reading from the file. They each try to
read 131072 bytes from the file (first parameter is the id, the second one
a pointer to the memory that the function reads to - the output shows the
first few bytes actually read, and parameter 3 is the number of bytes to
read). The returned value of 614 is the number of bytes actually read. The
second read() returns 0, which indicates nothing more to read, i.e. end of
file.

Well, and the last line finally closes the file.
This behavior is typically what a person would want. I'm fairly confident
it is not specified by the Standard(though I could well be wrong). There
are times when this is not what a programmer would want. For example
using embedded systems with limited memory, or very large AV files.
Right, but in such a situation, people typically resort to system specific
functions. Especially in the second case, you might not only want to switch
off the in-program buffering, but also open the file with another OS hard
disk cache strategy. And you have to read in reasonably sized chunks
yourself then, and the strategy you have to use for is often system
specific too. So I don't think that a standard stream would help in such a
situation.
OTOH, you can implement your own streambuf and use the standard streams with
it.
I suspect the same might apply when working with databases that hold all
of their data in a single file. I am certain that there were
(significant) performance differences between using input streams, and
buffered input streams in Java when transferring data over a network
connection. I'm fairly confident the same applies to file access.

That seems to suggest that Java offers finer grained control over I/O than
does C++ at the same level of abstraction. That finer control comes at
the cost of more complexity in the API. Typically we would expect just
the opposite when conparing Java and C++. I have less than 24 hours
experience dealing with this topic in C++ (and many of them were spent
sleeping) so I have much to learn before I can make a meaningful
assessment.


The C++ standard streams and streambuffers don't offer much flexibility in
this direction.

Jul 23 '05 #8

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

Similar topics

20
by: skoco | last post by:
Hello! Do you know when will be the new standard for c++ approved? And WHAT will be inside? Hope there will be some thread and synchro classes, text and XML parsing, new containers and other new...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
2
by: Jasim | last post by:
Hi, I'm a newbie C programmer, and I've written a Buffered File Copy program in C (which can be considered my rather first acceptable venture into the bold brave world of C) I am using...
9
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...
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
2
by: Karl | last post by:
Hey everyone! I've got a quick question on whether std::ifstream is buffered or not. The reason is that I have a homework assignment that requires me to benchmark copying files using different...
20
by: J de Boyne Pollard | last post by:
MThe library functions which are included to allow process Mlaunch, forking, and termination, imply that it is both Mpossible and desirable for a process to fork itself. This is Ma fundamental...
1
by: Clay Hobbs | last post by:
On Sun, 2008-10-05 at 14:16 -0400, Mike C. Fletcher wrote: I'm using wxPython. My real problem is that everything flashes when it moves. I thought the way to fix this was to make a...
8
by: zeeshan708 | last post by:
what is the difference between the buffered and unbuffered stream ??(e.g we say that cout is buffered and cerr is unbuffered stream) does that mean that the output sent to buffered stream have to go...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.