473,789 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redirect to STDOUT Problem

Trying to do something fairly routine... drop output into a file to
graph, but the following command at the bash command line:

ising output

produces a blinking cursor, an empty file named "output," and a
program that runs forever. With no redirect, the output is normal.
Below is the printf statement used to generate output.

printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f \
Chi=%5.2f\n",be ta,(energy/n2),(moment/n2), \
(summe2-summe*summe)/n2, \
(summm2-summm*summm)/n2);

I verified that things like 'ls output' work fine, and that I can
also redirect output from other c programs I've written. Something is
funny about this one.

Any thoughts?

Jun 27 '08 #1
18 2693
Jordan Glassman <jo************ @gmail.comwrite s:
Trying to do something fairly routine... drop output into a file to
graph, but the following command at the bash command line:

ising output
On modern systems, . is often not in your path. Did you just forget
to type ./ising?
produces a blinking cursor, an empty file named "output," and a
program that runs forever. With no redirect, the output is normal.
Below is the printf statement used to generate output.

printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f \
Chi=%5.2f\n",be ta,(energy/n2),(moment/n2), \
(summe2-summe*summe)/n2, \
(summm2-summm*summm)/n2);
Nothing here can explain what is happening. The printf is correct.
Of course, if n2 == 0 you won't get any output but that will happen
with and without the redirect.
I verified that things like 'ls output' work fine, and that I can
also redirect output from other c programs I've written. Something is
funny about this one.

Any thoughts?
The problem is probably not in the C code. You need someone to watch
exactly what you are doing and they'll probably spot it in no time. I
think a group like comp.unix.progr ammer might be better.

--
Ben.
Jun 27 '08 #2
Ben,

Thanks for the reply...
On modern systems, . is often not in your path. Did you just forget
to type ./ising?
No, that's not the problem.
Nothing here can explain what is happening. The printf is correct.
Of course, if n2 == 0 you won't get any output but that will happen
with and without the redirect.

The problem is probably not in the C code. You need someone to watch
exactly what you are doing and they'll probably spot it in no time. I
think a group like comp.unix.progr ammer might be better.
I tend to agree, but since *everything else* works except this
particular c program, which contains nothing more than a few
calculations inside loops and this one output statement, I decided to
post here first.

-j
Jun 27 '08 #3
Jordan Glassman wrote:
...
I tend to agree, but since *everything else* works except this
particular c program, which contains nothing more than a few
calculations inside loops and this one output statement, I
decided to post here first.
Though for some reason you decided not to post the code.
Still, your loss...

--
Peter
Jun 27 '08 #4
>
Though for some reason you decided not to post the code.
Still, your loss...
My loss? Here it is...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <curses.h>

#include <unistd.h// ANSI C???

#define N 40
#define NITERS 5000000

void filllattice(int (*m)[N]);
void seed(void); /* bad bad bad */
void printlattice(in t (*m)[N]);
void ising(int (*m)[N],double beta);

int main(){

int lattice[N][N];
double beta=0.0;

seed();

for(beta=0.5;be ta<=1.2;beta+=0 .01){

filllattice(lat tice);

// clear();

// printlattice(la ttice);

ising(lattice,b eta);

}

return 0;

}

void filllattice(int (*m)[N]){

int i,j;

for(i=0;i<N;i++ ){
for(j=0;j<N;j++ ){
if((((float)ran d()/(float)RAND_MAX )) 0.5) m[i][j]= 1;
else m[i][j] = -1;
}
}

}

void seed(void){

struct tm *mt;
int i;
time_t t;
char timestr[3];

t=time(NULL);
mt=localtime(&t );
strftime(timest r,3,"%S",mt);

srand(atoi(time str));

}

void printlattice(in t (*m)[N]){

int i,j;

/* system("clear") ; */
move(0,0);

for(i=0;i<N;i++ ){
putchar('\n');
for(j=0;j<N;j++ ){
if(m[i][j]==1)printf(" 1");
else printf(" -1");
}
}

putchar('\n');
}

void ising(int (*m)[N],double beta){

int i,j,k;
double energy,moment;
double summe,summm,sum me2,summm2;
int numsteps=0;
double sum_nn,sums;
double deltaH;
double sum_nnn;
int n2=N*N;
double half=NITERS/2;

k=0;

energy=moment=0 .0;
summe=summm=sum me2=summm2=0.0;

// pick a site randomly

for(i=0;i<N;i++ ){
for(j=0;j<N;j++ ){
moment += m[i][j];
energy -= 2*m[i][j]*(m[(i+1)%N][j]+m[i][(j+1)%N]+m[(i+1)%N][(j
+1)%N]/5);
}
}

for(k=0;k<NITER S;k++){

i=rand()%N;
j=rand()%N;

sum_nn=m[i][((j+1)%N)]+m[i][((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
%N)][j];
sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
+m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
sums=sum_nn+sum _nnn;
deltaH=m[i][j]*sums;
if((exp(-1.3*deltaH*beta ))>((float)rand ()/(float)RAND_MAX ) ||
(deltaH < 0) ){
energy += 2*deltaH;
moment -= 2*m[i][j];
m[i][j] *= -1;
}

if(k>half){
summe+=energy;
summe2+=energy* energy;
summm+=moment;
summm2+=moment* moment;
}

/* if(k>10000){
k=0;
putchar('\n');
printf("E=%f m=%f",(energy/n2),(moment/n2));
printlattice(m) ;
sleep(1);
} */

}

summe /= half;
summe2 /= half;
summm /= half;
summm2 /= half;

printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",be ta,(energy/
n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

}

Jun 27 '08 #5
Jordan Glassman said:
>>
Though for some reason you decided not to post the code.
[...]
[...] Here it is...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <curses.h>

#include <unistd.h// ANSI C???
This // comment really screwed up my compiler. To answer your question
redoubled in spades, neither curses.h nor unistd.h are ANSI C. // comments
have become so officially (since 1999), but in practice almost nobody has
a compiler that conforms to C99 in *every* respect, so those of us who
prefer to use a conforming mode have no choice but to conform to an
earlier standard (C90 or C95), in which // is merely a syntax error. In
this case, my compiler saw // ANSI C??? as part of the filename to
include!

Anyway, that's easy enough to fix (I removed the comment!), but doing so
exposed a whole bunch of compilation issues with your code - dozens of
errors. Oddly, however, removing or replacing the remaining // comments
sorted most of these out. The remaining nits were:
int main(){
My compiler (rightly) prefers int main(void) rather than just int main(),
but really this is no big deal. You have the return type (int) exactly
right, and that's what matters here.
if((((float)ran d()/(float)RAND_MAX )) 0.5)
The compiler says: "warning: cast does not match function type"

In almost all cases, a cast on a human being indicates a broken limb.
In almost all cases, a cast in a C program indicates a broken line.

In C, the normal way to do (pseudo) random numbers selected from N
possibilities (in the range 0 to N-1) is to get a value in the half-open
range [0, 1) like this:

rand() / (RAND_MAX + 1.0)

and then multiply by N to get a value in the half-open range [0, N).
It seems that you want a value in the half-open range [0, 1) so you can
omit the multiplication step:

if(rand() / (RAND_MAX + 1.0) 0.5)

(This happens in one other place in the code, too.)

Running this code gives me a bunch of results as follows:

B=0.50 E= -0.8500 m= -0.0325 Cv= 3.52 Chi= 9.21
B=0.51 E= -0.8225 m= 0.1338 Cv= 3.42 Chi=10.27
B=0.52 E= -0.8500 m= -0.1550 Cv= 3.65 Chi=13.73
B=0.53 E= -0.8100 m= 0.0325 Cv= 3.69 Chi=16.82
B=0.54 E= -0.8975 m= 0.0488 Cv= 3.72 Chi=13.67
B=0.55 E= -0.9200 m= -0.0088 Cv= 4.08 Chi=20.47
B=0.56 E= -0.9975 m= 0.0563 Cv= 4.18 Chi=19.43
B=0.57 E= -0.9525 m= 0.0125 Cv= 4.59 Chi=34.67
B=0.58 E= -1.1075 m= -0.1938 Cv= 4.12 Chi=22.44
B=0.59 E= -1.0425 m= 0.1288 Cv= 4.83 Chi=30.55
B=0.60 E= -1.0675 m= -0.1750 Cv= 5.23 Chi=38.14
B=0.61 E= -1.0475 m= -0.0775 Cv= 5.36 Chi=55.96
B=0.62 E= -1.1625 m= 0.2350 Cv= 4.85 Chi=87.07
B=0.63 E= -1.1850 m= -0.0050 Cv= 6.26 Chi=127.39

but it didn't appear to stop. I reduced NITERS from 5000000 to 5 and N from
40 to 4, and it worked just fine, producing 70 lines of output.
Redirection of stdout to a file named 'output' worked just fine with no
weirdities going on. Looks fine to me.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #6
Jordan Glassman <jo************ @gmail.comwrite s:
>Though for some reason you decided not to post the code.
Still, your loss...

My loss? Here it is...
Yes. I can now tell you how to get it to work (but
comp.unix.progr ammer is still the better place).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <curses.h>
curses programs can't usually have their put redirected (ask in
c.u.p for when and why).
#include <unistd.h// ANSI C???
No, not ANSI C.

<snip>
void seed(void){

struct tm *mt;
int i;
time_t t;
char timestr[3];

t=time(NULL);
mt=localtime(&t );
strftime(timest r,3,"%S",mt);

srand(atoi(time str));

}
The comp.lang.c FAQ has some advice seeding rand().
http://c-faq.com/lib/srand.html
void printlattice(in t (*m)[N]){

int i,j;

/* system("clear") ; */
move(0,0);
Comment this out. You don't need the curses functionality (at least
not yet). If you do more with it in future versions, it may be worth
the price of losing output re-direction.
for(i=0;i<N;i++ ){
putchar('\n');
for(j=0;j<N;j++ ){
if(m[i][j]==1)printf(" 1");
else printf(" -1");
}
}

putchar('\n');
}

void ising(int (*m)[N],double beta){

int i,j,k;
double energy,moment;
double summe,summm,sum me2,summm2;
int numsteps=0;
double sum_nn,sums;
double deltaH;
double sum_nnn;
int n2=N*N;
double half=NITERS/2;

k=0;

energy=moment=0 .0;
summe=summm=sum me2=summm2=0.0;

// pick a site randomly

for(i=0;i<N;i++ ){
for(j=0;j<N;j++ ){
moment += m[i][j];
energy -= 2*m[i][j]*(m[(i+1)%N][j]+m[i][(j+1)%N]+m[(i+1)%N][(j
+1)%N]/5);
}
}

for(k=0;k<NITER S;k++){

i=rand()%N;
j=rand()%N;

sum_nn=m[i][((j+1)%N)]+m[i][((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
%N)][j];
sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
+m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
sums=sum_nn+sum _nnn;
deltaH=m[i][j]*sums;
if((exp(-1.3*deltaH*beta ))>((float)rand ()/(float)RAND_MAX ) ||
(deltaH < 0) ){
energy += 2*deltaH;
moment -= 2*m[i][j];
m[i][j] *= -1;
}

if(k>half){
summe+=energy;
summe2+=energy* energy;
summm+=moment;
summm2+=moment* moment;
}

/* if(k>10000){
k=0;
putchar('\n');
printf("E=%f m=%f",(energy/n2),(moment/n2));
printlattice(m) ;
sleep(1);
} */

}

summe /= half;
summe2 /= half;
summm /= half;
summm2 /= half;

printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",be ta,(energy/
n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);
Add:

fflush(stdout);

to be sure to get what data you have written so far. I did not wait for
the program to finish -- if you do there is no need to call fflush.

--
Ben.
Jun 27 '08 #7
Ben Bacarisse <be********@bsb .me.ukwrites:
curses programs can't usually have their put redirected (ask in
c.u.p for when and why).
Oh the perils of off-topic answers. Please don't get into this here.
I know it is wrong -- I was over-simplifying to the point of being
plain wrong. Berate me in comp.unix.progr ammer if you must!

--
Ben.
Jun 27 '08 #8
Ben,

Taking out the curses.h and all associated functions did it. I'm
still not sure why the printf didn't work, though... not of the
curses.h functions were actually being used in that code I posted...
they were all commented out! It's as if curses replaces printf with
another version.

Dunno. In any case, thanks for the help.

-j

On May 12, 9:01 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Jordan Glassman <jordanglass... @gmail.comwrite s:
Though for some reason you decided not to post the code.
Still, your loss...
My loss? Here it is...

Yes. I can now tell you how to get it to work (but
comp.unix.progr ammer is still the better place).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <curses.h>

curses programs can't usually have their put redirected (ask in
c.u.p for when and why).
#include <unistd.h// ANSI C???

No, not ANSI C.

<snip>
void seed(void){
struct tm *mt;
int i;
time_t t;
char timestr[3];
t=time(NULL);
mt=localtime(&t );
strftime(timest r,3,"%S",mt);
srand(atoi(time str));
}

The comp.lang.c FAQ has some advice seeding rand().http://c-faq.com/lib/srand.html
void printlattice(in t (*m)[N]){
int i,j;
/* system("clear") ; */
move(0,0);

Comment this out. You don't need the curses functionality (at least
not yet). If you do more with it in future versions, it may be worth
the price of losing output re-direction.
for(i=0;i<N;i++ ){
putchar('\n');
for(j=0;j<N;j++ ){
if(m[i][j]==1)printf(" 1");
else printf(" -1");
}
}
putchar('\n');
}
void ising(int (*m)[N],double beta){
int i,j,k;
double energy,moment;
double summe,summm,sum me2,summm2;
int numsteps=0;
double sum_nn,sums;
double deltaH;
double sum_nnn;
int n2=N*N;
double half=NITERS/2;
k=0;
energy=moment=0 .0;
summe=summm=sum me2=summm2=0.0;
// pick a site randomly
for(i=0;i<N;i++ ){
for(j=0;j<N;j++ ){
moment += m[i][j];
energy -= 2*m[i][j]*(m[(i+1)%N][j]+m[i][(j+1)%N]+m[(i+1)%N][(j
+1)%N]/5);
}
}
for(k=0;k<NITER S;k++){
i=rand()%N;
j=rand()%N;
sum_nn=m[i][((j+1)%N)]+m[i][((j-1+N)%N)]+m[((i+1)%N)][j]+m[((i-1+N)
%N)][j];
sum_nnn=(m[((i+1)%N)][((j+1)%N)]+m[((i-1+N)%N)][((j-1+N)%N)]
+m[((i-1+N)%N)][((j+1)%N)]+m[((i+1)%N)][((j-1+N)%N)])/5;
sums=sum_nn+sum _nnn;
deltaH=m[i][j]*sums;
if((exp(-1.3*deltaH*beta ))>((float)rand ()/(float)RAND_MAX ) ||
(deltaH < 0) ){
energy += 2*deltaH;
moment -= 2*m[i][j];
m[i][j] *= -1;
}
if(k>half){
summe+=energy;
summe2+=energy* energy;
summm+=moment;
summm2+=moment* moment;
}
/* if(k>10000){
k=0;
putchar('\n');
printf("E=%f m=%f",(energy/n2),(moment/n2));
printlattice(m) ;
sleep(1);
} */
}
summe /= half;
summe2 /= half;
summm /= half;
summm2 /= half;
printf("B=%4.2f E=%8.4f m=%8.4f Cv=%5.2f Chi=%5.2f\n",be ta,(energy/
n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

Add:

fflush(stdout);

to be sure to get what data you have written so far. I did not wait for
the program to finish -- if you do there is no need to call fflush.

--
Ben.
Jun 27 '08 #9
Jordan Glassman wrote:
Ben,

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Jun 27 '08 #10

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

Similar topics

6
5640
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* . I have tried the simplest method of capturing stdout, stderr via: saveout = sys.stdout sys.stdout = file_obj
3
3295
by: legba | last post by:
hi all.. I'm writing an application in python2.3 under linux debian which accepts new "plug-ins" in the form of c-written python extension modules. My problem is that I'd like to catch the stdout/stderr from one of this modules and redirect it into a tkinter text widget, in real time. Let's say the module's name is plugin and that plugin.doSomething() prints out with a C printf() the string "it's a mess".
10
21142
by: Michael Gaab | last post by:
If I redirect stdout by using freopen("afile", "w", stdout); and then I closed stdout using, fclose(stdout), essentially I am just closing "afile". I have to reestablish what stdout originally pointed to? thanks
2
3701
by: 28tommy | last post by:
Hi there, I'm doing some TELNET and FTP sessions with my scripts. I need to redirect all the output (and not just what I print) to an output file, but still be able to see the session in process on the console. The Console screen is of less importance, so I could give it up, but what I'm looking for is a way to see all the interaction with the remote seesions on a file, that all the console data was redirected to it from within the...
1
2706
by: abcd | last post by:
I have a program which is written in C and interfaced with python via Swig. However, the function I call prints stuff out to the console. I would like to capture what it is printing out. I tried: import MyCProg, sys f = open("tmp.txt", "wb") sys.stdout = f MyCProg.getData()
3
3105
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out of buffer space. Once I start reading the stdout file returned by os.popen2 then the process resumes. I know that I could just specify > /dev/null when starting the process but I'd like to know if there is a way to start a process using os.popen2...
6
3322
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
5
21074
by: ws.taylor | last post by:
I am in a systems programming class, using C on a Solaris 10 development server. Part of the first programming assignment calls for the output of a command executed by a child process with execvp to be redirected to a buffer so that it can be sent through a named pipe back to the parent process. The only part I'm having problems with is capturing execvp's output to a buffer. I can't find anything about this from our book, or from searching...
3
6149
by: Alejandro | last post by:
Hi: I want to redirect stdout to a textctrl I have. From what I read in the wxpython documentation, I can use the wxLogTextCtrl class to do this. I am doing the following: class MyGui(gui.MyFrame): #gui.MyFrame generated by wxGlade def __init__(self, *args, **kwds): gui.MyFrame.__init__(self, *args, **kwds) # ... code removed ...
10
8427
by: Jef Driesen | last post by:
I wrote a program that writes a large amount of information to stdout (and stderr). When run from the commandline, this output either appears on the console window (the default) or can be redirected to a file by means of the freopen() function: freopen ("logfile.txt", "w", stdout); But I would like to have the output on the console AND a logfile. Is this possible (without using an extra program like 'tee')?
0
9511
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
10404
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...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
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
9016
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
7525
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
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
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
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.