473,405 Members | 2,272 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,405 software developers and data experts.

What does istream::sync()?

I am reading TC++PL3 and on page 644 it is mentioned:

"Flushing an istream is done using sync(). This cannot always be done
right. For some kinds of streams, we would have to reread characters
from the real source - and that is not always possible or desirable.
Consequently, sync() returns 0 if it succeeded. If it failed, it sets
ios_base::badbit (21.3.3) and returns -1. Again, setting badbit might
trigger an exception (21.3.6). A sync() on a buffer attached to an
ostream flushes the buffer to output".
So, what exactly should we expect from an call of sync() on an istream?
Making its streambuf to lose all its contents?

The code:

#include <iostream>
int main()
{
using namespace std;

char c;

cin>c;

cout<< c<< endl;

int sync_failure= cin.sync();

cout<< sync_failure<< endl;

cin >c;

cout<< c<< endl;
}
in my system produces:

[john@localhost src]$ ./foobar-cpp
test
t
0
e

[john@localhost src]$
Oct 6 '07 #1
7 5186
john wrote:
....
So, what exactly should we expect from an call of sync() on an istream?
Making its streambuf to lose all its contents?
....

istream is allowed to buffer input. If it does so, it will not be able
to see changes to the parts of the file in it's input buffer after the
buffer is filled. What sync appears to do is to expire the content of
the buffer if the input is appropriate (i.e. file based input).

Below is an strace of your code with the source as input. Notice that
it reads the file into memory and after the sync it reads it again but
offset by 1. If after the first read the file had changed, then the
second read would expose the new contents of the file.

read(0, "#include <iostream>\n\n\nint main()"..., 4096) = 241
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 4), ...}) = 0
write(1, "#\n", 2#) = 2
lseek(0, -240, SEEK_CUR) = 1
write(1, "0\n", 20) = 2
read(0, "include <iostream>\n\n\nint main()\n"..., 4096) = 240
write(1, "i\n", 2i) = 2
exit_group(0) = ?
Oct 6 '07 #2
On Oct 6, 1:58 pm, john <j...@no.spamwrote:
I am reading TC++PL3 and on page 644 it is mentioned:
"Flushing an istream is done using sync(). This cannot always be done
right. For some kinds of streams, we would have to reread characters
from the real source - and that is not always possible or desirable.
Consequently, sync() returns 0 if it succeeded. If it failed, it sets
ios_base::badbit (21.3.3) and returns -1. Again, setting badbit might
trigger an exception (21.3.6). A sync() on a buffer attached to an
ostream flushes the buffer to output".
So, what exactly should we expect from an call of sync() on an istream?
Nothing. The standard leaves it entirely up to the
implementation; different implementations do different things.
Making its streambuf to lose all its contents?
Well, it certainly depends on the type of the streambuf; for a
stringbuf, for example, sync is always a no-op. For a file buf,
sync() on input isn't specified by the standard.

Some implementations, I think, simply throw out any data they
happen to have in the buffer. *IF* the implementation is line
buffering the file (as do Unix, and I think Windows, when the
data source is a keyboard), this has more or less the same
effect as ignoring input up to and including the next newline.
Don't use it for this, however, since it will fail if the input
is not line buffered (not from the keyboard). And of course,
it's not standard; other implementations (e.g. g++) simply treat
the sync as a no-op.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 7 '07 #3
On Oct 6, 10:42 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
john wrote:
...So, what exactly should we expect from an call of sync() on an istream?
Making its streambuf to lose all its contents?
...
istream is allowed to buffer input. If it does so, it will not be able
to see changes to the parts of the file in it's input buffer after the
buffer is filled. What sync appears to do is to expire the content of
the buffer if the input is appropriate (i.e. file based input).
That would be logical, wouldn't it. The standard simply says:
"If a get area exists, the effect is implementation defined."
So basically, you can't count on anything. (Practically, of
course, what is "appropriate" for a filebuf, which may be
connected to a tty, a file or even maybe a pipe.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 7 '07 #4
James Kanze wrote:
>
other implementations (e.g. g++) simply treat
the sync as a no-op.

I am not sure what you mean by this. I am using g++ under Linux and it
doesn't complain for "cin.sync()".
Oct 7 '07 #5
john a écrit :
James Kanze wrote:
>>
other implementations (e.g. g++) simply treat
the sync as a no-op.


I am not sure what you mean by this. I am using g++ under Linux and it
doesn't complain for "cin.sync()".
No-op simply means it doesn't do anything (empty function or removed by
the compiler).

Michael
Oct 7 '07 #6
john wrote:
James Kanze wrote:
>>
other implementations (e.g. g++) simply treat
the sync as a no-op.


I am not sure what you mean by this. I am using g++ under Linux and it
doesn't complain for "cin.sync()".
I'm not sure either since the strace I showed earlier is from a Linux
system. However don't expect your compiler to complain if somthing is
implemented as a no-op.
Oct 7 '07 #7
On Oct 7, 11:34 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
john wrote:
James Kanze wrote:
other implementations (e.g. g++) simply treat
the sync as a no-op.
I am not sure what you mean by this. I am using g++ under Linux and it
doesn't complain for "cin.sync()".
I'm not sure either since the strace I showed earlier is from a Linux
system. However don't expect your compiler to complain if somthing is
implemented as a no-op.
The g++ behavior seems to depend on the system; I get different
behavior under Solaris and Linux. IMHO, it would also be
reasonable for the behavior to be what you describe if the input
is from a regular file, but to be a no-op is isatty is true, or
if the input is from a pipe (named---since I don't think the
current implementation of g++ has any means of getting an
istream from a pipe).

At any rate, the behavior is definitely variable; if I do
something like:

std::cin >aChar ;
std::cin.rdbuf()->pubsync() ;
std::cin >aString ;

then input "abcd<RETURN>123", followed by a return and the
system end of file character (^D under Unix, ^Z under Windows),
I'll get "bcd" in aString on some implementations (Sun
CC/Solaris, g++/Linux), "123" on others
(g++/Solaris,VC++/Windows). IIRC, "123" corresponds to the
traditional implementation; what the classical iostream from USL
did. (And of course, a lot of Windows programmers think that
that's what it always does, because that's what VC++ does.)

For a tty, of course. If I put the same data in a file, and
redirect input from that, I get something different with
g++/Solaris and VC++/Windows: "bcd" with g++/Solaris and an
empty string with VC++. The only way I can explain the g++
behavior in this case is that the implementation detects if the
input is a regular file (or not a tty, or something), and
behaves differently. (A very reasonable choice, thinking about
it. Seek back the number of characters in your buffer and throw
out the contents of the buffer if seeking will work; no-op if it
won't. But why don't I get this under Linux, then? My Solaris
and my Linux version were generated from the exact same source
tree.) VC++'s behavior, of course, is exactly what you get by
just throwing out any contents---keyboard input is line buffered
by the system, so you throw out through the end of the line,
file input isn't, so you throw out (in this case) a lot more.

Anyway, I think we agree that there's nothing here you can count
on in portable code.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 8 '07 #8

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

Similar topics

3
by: Derek Fountain | last post by:
The documentation says session_destroy() "destroys all of the data associated with the current session". Um, like what? The docs further say that you should remove all information in the _SESSION...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
2
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled...
0
arunmib
by: arunmib | last post by:
Hi all, I have doubt, as what does __declspec (dllimport) or __declspec(dllexport) does? If add these only the functions of a dll are exported or imported right? So what exactly does...
9
by: JoeC | last post by:
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth; m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight; What does this mean? I have seen v=&var->member.thing; but what does it mean when you...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
9
by: James Dow Allen | last post by:
How about this idea? Post fragments of C code which seem fun, interesting or instructive. Puzzles can be posed in various ways. (What does this do? Can you see the bug? How to code this for...
3
by: qianz99 | last post by:
Hi I am not sure what this code does. I have the following questions 1. where is the case? 2. #define TLV_INTEGER(name, octets) p->name = -1; Is it define a function TLV_INTEGER(name, octets) ...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
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,...
0
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...
0
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,...

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.