Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Redirect to STDOUT Problem

Reply
Thread Tools

Redirect to STDOUT Problem

 
 
Jordan Glassman
Guest
Posts: n/a
 
      05-13-2008
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",beta,(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?



 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-13-2008
Jordan Glassman <(E-Mail Removed)> writes:

> 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",beta,(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.programmer might be better.

--
Ben.
 
Reply With Quote
 
 
 
 
Jordan Glassman
Guest
Posts: n/a
 
      05-13-2008
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.programmer 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      05-13-2008
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
 
Reply With Quote
 
Jordan Glassman
Guest
Posts: n/a
 
      05-13-2008
>
> 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(int (*m)[N]);
void ising(int (*m)[N],double beta);

int main(){

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

seed();

for(beta=0.5;beta<=1.2;beta+=0.01){

filllattice(lattice);

// clear();

// printlattice(lattice);

ising(lattice,beta);

}

return 0;

}

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

int i,j;

for(i=0;i<N;i++){
for(j=0;j<N;j++){
if((((float)rand()/(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(timestr,3,"%S",mt);

srand(atoi(timestr));

}

void printlattice(int (*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,summe2,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=summe2=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<NITERS;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",beta,(energy/
n2),(moment/n2),(summe2-summe*summe)/n2,(summm2-summm*summm)/n2);

}



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-13-2008
Jordan Glassman <(E-Mail Removed)> writes:

>> 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.programmer 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(timestr,3,"%S",mt);
>
> srand(atoi(timestr));
>
> }


The comp.lang.c FAQ has some advice seeding rand().
http://c-faq.com/lib/srand.html

> void printlattice(int (*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,summe2,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=summe2=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<NITERS;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",beta,(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.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-13-2008
Ben Bacarisse <(E-Mail Removed)> writes:

> 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.programmer if you must!

--
Ben.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      05-13-2008
[Other replies notwithstanding...]

Jordan Glassman wrote:
> #define NITERS 5000000
> int i,j,k;
> for(k=0;k<NITERS;k++){


Are you perchance running on a machine with 16-bit ints?

If so, this loop will likely run forever (assuming 16-bit overflow
from INT_MAX to INT_MIN.)

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


Identifiers starting with is (or to) and a lowercase letter are
reserved for use as external identifiers.

This is not likely to be the source of your problem, but it is
still a fault with your code.

Apart from that, the program does take some time to run.
Are you leaving it for long enough? With such relatively
small output, many systems won't actually flush to the
redirected file until the program (and stream) closes.

--
Peter
 
Reply With Quote
 
Jordan Glassman
Guest
Posts: n/a
 
      05-13-2008
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 <(E-Mail Removed)> wrote:
> Jordan Glassman <(E-Mail Removed)> writes:
> >> 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.programmer 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(timestr,3,"%S",mt);

>
> > srand(atoi(timestr));

>
> > }

>
> The comp.lang.c FAQ has some advice seeding rand().http://c-faq.com/lib/srand.html
>
> > void printlattice(int (*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,summe2,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=summe2=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<NITERS;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",beta,(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.


 
Reply With Quote
 
Jordan Glassman
Guest
Posts: n/a
 
      05-13-2008
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 <(E-Mail Removed)> wrote:
> Jordan Glassman <(E-Mail Removed)> writes:
> >> 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.programmer 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(timestr,3,"%S",mt);

>
> > srand(atoi(timestr));

>
> > }

>
> The comp.lang.c FAQ has some advice seeding rand().http://c-faq.com/lib/srand.html
>
> > void printlattice(int (*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,summe2,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=summe2=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<NITERS;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",beta,(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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Basic Q - Response.Redirect, all redirect to first Response.Redirect statement Sal ASP .Net Web Controls 1 05-15-2004 03:46 PM
redirect stdout & stderr tomcat 4.1 VisionSet Java 6 09-20-2003 01:37 AM



Advertisments