Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Redirect to STDOUT Problem (http://www.velocityreviews.com/forums/t614489-redirect-to-stdout-problem.html)

Jordan Glassman 05-13-2008 12:50 AM

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",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?




Ben Bacarisse 05-13-2008 01:14 AM

Re: Redirect to STDOUT Problem
 
Jordan Glassman <jordanglassman@gmail.com> 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.

Jordan Glassman 05-13-2008 01:21 AM

Re: Redirect to STDOUT Problem
 
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

Peter Nilsson 05-13-2008 02:33 AM

Re: Redirect to STDOUT Problem
 
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

Jordan Glassman 05-13-2008 02:46 AM

Re: Redirect to STDOUT Problem
 
>
> 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);

}




Ben Bacarisse 05-13-2008 04:01 AM

Re: Redirect to STDOUT Problem
 
Jordan Glassman <jordanglassman@gmail.com> 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.

Ben Bacarisse 05-13-2008 04:05 AM

Re: Redirect to STDOUT Problem
 
Ben Bacarisse <ben.usenet@bsb.me.uk> 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.

Peter Nilsson 05-13-2008 04:23 AM

Re: Redirect to STDOUT Problem
 
[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

Jordan Glassman 05-13-2008 05:05 AM

Re: Redirect to STDOUT Problem
 
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.uk> wrote:
> Jordan Glassman <jordanglass...@gmail.com> 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.



Jordan Glassman 05-13-2008 05:54 AM

Re: Redirect to STDOUT Problem
 
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.uk> wrote:
> Jordan Glassman <jordanglass...@gmail.com> 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.




All times are GMT. The time now is 05:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.