 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
joel Guest
|
Posted: Wed Mar 09, 2005 11:10 am Post subject: printf() |
|
|
Hi,
sometimes I see some weird stuff happening in pre-written code (by
other person). Let's say there are two simple printf() as below:
printf("nstatement 1");
printf("nstatement 2");
the first one gets printed during run time but the second doesn't !!!
The only thing I can think of is being a second process involved in
this mess. since it is a program under uinx. Do you have any
suggestions on this?
Will be appreciated greatly.
Thanks,
Joel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
JackSpades Guest
|
Posted: Wed Mar 09, 2005 11:18 pm Post subject: Re: printf() |
|
|
In general (at least on UNIX) output will get cached until there is a
newline character. Thus the n from the second line will cause the
first line to actually get printed to the terminal, but there is no n
to flush the second line out.
adding:
fflush( stdout );
will force the line to output.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Wed Mar 09, 2005 11:34 pm Post subject: Re: printf() |
|
|
joel wrote:
| Quote: | Hi,
sometimes I see some weird stuff happening in pre-written code (by
other person). Let's say there are two simple printf() as below:
printf("nstatement 1");
printf("nstatement 2");
the first one gets printed during run time but the second doesn't !!!
The only thing I can think of is being a second process involved in
this mess. since it is a program under uinx. Do you have any
suggestions on this?
|
It could be that the buffers are not being flushed, or are being
cleared in between.
If I recall correctly, you can turn off buffering with
setbuf(stdout, 0).
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
simont Guest
|
Posted: Wed Mar 09, 2005 11:40 pm Post subject: Re: printf() |
|
|
Are you sure stdout is getting flushed after the second printf? That
text could be languishing in a buffer.
Try putting "fflush(stdout);" after the second statement, or just
sticking an 'n' at the end of the second string.
Might be able to tell for sure, of course, if you'd posted an entire
minimal program exhibiting this behaviour ...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Wed Mar 09, 2005 11:43 pm Post subject: Re: printf() |
|
|
joel <ramsin.savra (AT) gmail (DOT) com> wrote:
| Quote: | sometimes I see some weird stuff happening in pre-written code (by
other person). Let's say there are two simple printf() as below:
printf("nstatement 1");
printf("nstatement 2");
the first one gets printed during run time but the second doesn't !!!
The only thing I can think of is being a second process involved in
this mess. since it is a program under uinx. Do you have any
suggestions on this?
|
stdout is (line) buffered, so lines won't appear until ended with n
and/or the stream is flushed. See man stdin(3) for details.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dietmar_kuehl@yahoo.com Guest
|
Posted: Thu Mar 10, 2005 9:23 am Post subject: Re: printf() |
|
|
joel wrote:
| Quote: | the first one gets printed during run time but the second doesn't !!!
|
Both stdio and IOStreams buffer the input and output. What you see
is that the output to stdout is buffered. You can force the buffer
to be flushed using 'fflush()'. The stdio specification knows three
kinds of buffering:
- _IOFBF: fully buffered, i.e. the buffer is flushed when it becomes
full
- _IOLBF: line buffered, i.e. the buffer is flushed when a newline
is received or the buffer becomes full
- _IONBF: no buffering, i.e. there is no buffer.
In general, the program should flush the buffer when the stream is
closed and probably also at program termination. However, when the
program crashes, the buffer is, of course, not flushed.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Samuel Krempp Guest
|
Posted: Sun Mar 13, 2005 12:31 pm Post subject: Re: printf() |
|
|
le Wednesday 09 March 2005 12:10, [email]ramsin.savra (AT) gmail (DOT) com[/email] écrivit :
| Quote: | printf("nstatement 1");
printf("nstatement 2");
the first one gets printed during run time but the second doesn't !!!
The only thing I can think of is being a second process involved in
this mess. since it is a program under uinx. Do you have any
suggestions on this?
|
sorry if this sounds trivial, but if the last printf isnt ended by a new
line, the terminal might hide the text, even if flushed.
#include "stdio.h"
int main() {
printf("ligne 1n");
printf("reste");
fflush(stdin);
}
marvin% ./a.out
ligne 1
marvin%
You can still get the text, e.g. this way :
marvin% ./a.out; echo
ligne 1
reste
marvin%
anyway, this is not really C++..
--
Samuel.Krempp
cout << "@" << "crans." << (is_spam ? "trucs.en.trop." : "" )
<< "ens-cachan.fr" << endl;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Samuel Krempp Guest
|
Posted: Mon Mar 14, 2005 12:04 am Post subject: Re: printf() |
|
|
le Sunday 13 March 2005 13:31, [email]krempp (AT) crans (DOT) ens-cachan.fr[/email] écrivit :
| Quote: | #include "stdio.h"
int main() {
printf("ligne 1n");
printf("reste");
fflush(stdin);
|
oops, that last line should have been :
fflush(stdout);
obviously...
(though, it doesnt change anything in practise, as I was saying).
--
Samuel.Krempp
cout << "@" << "crans." << "ens-cachan.fr" << endl;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Phillip Jordan Guest
|
Posted: Mon Apr 18, 2005 3:10 pm Post subject: Re: printf() |
|
|
joel wrote:
| Quote: | sometimes I see some weird stuff happening in pre-written code (by
other person). Let's say there are two simple printf() as below:
printf("nstatement 1");
printf("nstatement 2");
|
This isn't a valid C++ program.
In case you meant this:
//////////////////////////////////
#include <cstdio>
int main()
{
printf("nstatement 1");
printf("nstatement 2");
return 0;
}
//////////////////////////////////
and this doesn't give the expected result, I would suspect a problem
with your compiler, standard library, operating system, or hardware.
| Quote: | The only thing I can think of is being a second process involved in
this mess. since it is a program under uinx. Do you have any
suggestions on this?
|
Other processes don't generally have access to your process's memory
space, unless you're running this on hardware or an operating system
that doesn't support separate memory spaces.
If that's not the case, and your build environment is fine, you haven't
posted enough code.
~phil
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Samuel Rødal Guest
|
Posted: Wed Apr 20, 2005 9:35 am Post subject: Re: printf() |
|
|
On Mon, 18 Apr 2005 17:10:55 +0200, Phillip Jordan
<phillip.m.jordan (AT) gmail (DOT) com> wrote:
| Quote: | joel wrote:
sometimes I see some weird stuff happening in pre-written code (by
other person). Let's say there are two simple printf() as below:
printf("nstatement 1");
printf("nstatement 2");
This isn't a valid C++ program.
In case you meant this:
//////////////////////////////////
#include
int main()
{
printf("nstatement 1");
printf("nstatement 2");
return 0;
}
//////////////////////////////////
and this doesn't give the expected result, I would suspect a problem
with your compiler, standard library, operating system, or hardware.
|
Shouldn't that be std::printf?
--
Samuel Rødal
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|