CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   FLUENT (http://www.cfd-online.com/Forums/fluent/)
-   -   int to char in UDF (http://www.cfd-online.com/Forums/fluent/50011-int-char-udf.html)

Paolo Lampitella December 9, 2008 16:12

int to char in UDF
 
Hi,

i wrote the following UDF to write some simple data on an external file:

/*****UDF for output*****/

#include "udf.h"

DEFINE_ON_DEMAND(output)

{

Domain *d;

real x[ND_ND];

real x0, y0, z0, u, v, w;

real p, mut;

int time;

char *filename;

FILE *fp;

Thread *t;

cell_t c;

d = Get_Domain(1);

time = N_TIME;

filename = "data.txt";

fp = fopen(filename,"w");

thread_loop_c(t,d)

{

begin_c_loop(c,t)

{

C_CENTROID(x,c,t);

ND_SET(x0, y0, z0, x[0], x[1], x[2]);

ND_SET(u, v, w, C_U(c, t), C_V(c, t), C_W(c, t));

p = C_P(c,t);

mut = C_MU_T(c,t);

fprintf(fp, "%14.12f %14.12f %14.12f ", x0, y0, z0);

fprintf(fp, "%18.16f %18.16f %18.16f %18.16f %18.16f\n", u, v, w, p, mut);

}

end_c_loop(c,t)

}

fclose(fp);

}

Now, i'd like to modify filename to handle the the current number of time steps, i.e.

data-2000.txt

data-3000.txt

and so on, but i'm not able to do so. Actually i think that the only line which has to be modified is

filename = "data.txt";

but i don't know how because all i know about c is coming from the fluent udf manual. Any help is really appreciated.

Thanks

max December 10, 2008 09:53

Re: int to char in UDF
 
hello paolo

sprintf() will do the job, e.g.:

const char base[] = "data-"; char filename [10]; int number = RP_Get_Integer("time-step"); sprintf(filename, "%s%d", base, number);

you may check this with: printf("filename = \"%s\"\n", filename);

cheers max


Paolo Lampitella December 10, 2008 13:51

Re: int to char in UDF
 
Thanks max for your help but i'm not able to let it work.

Moreover sprintf seems not be recognized...maybe it is because i'm interpreting the udf

Somewhere i also find the itoa command but i really don't know how to use it. Anyway, thanks again for your help.

max December 11, 2008 05:01

Re: int to char in UDF
 
All i can say is that sprintf works nicely in compiled udf mode. Give it a try and let me know if the problem persists.

max


All times are GMT -4. The time now is 13:06.