CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   FLUENT (https://www.cfd-online.com/Forums/fluent/)
-   -   FSCANF Problem (https://www.cfd-online.com/Forums/fluent/45683-fscanf-problem.html)

DK August 10, 2007 10:07

FSCANF Problem
 
I'm trying to load velocity data from a file using the following code:

/***********BEGIN**************/

int maxcent = 1;

int a = 0;

int b = 0;

double tempvel;

double velprof[1000][8]; /* make sure # faces <= 1000 */

double test1;

FILE *fi2;

fi2 = fopen("velprof4.txt", "r");

if (fi2 == NULL)

printf("*\nERROR - CANNOT OPEN THE FILE\n*");

else

printf("*\n File Opened. ID is %d\n", fi2);

while (a <= maxcent)

{

b = 0;

while (b <= 7)

{

fscanf(fi2, "%g", &tempvel);

velprof[a][b] = tempvel;

printf("%d %d %g\n", a, b, velprof[a][b]);

++b;

}

++a;

}

fclose(fi2); } /***********END**************/

The input file was created in a text editor and is: 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0; 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0;

The representative output from FLUENT for the velocity matrix is:

File Opened. ID is 18001056;

0 0 5.26354e-315

0 1 5.30499e-315

0 2 5.32571e-315

0 3 5.34643e-315

0 4 5.3568e-315

0 5 5.36716e-315

0 6 5.37752e-315

0 7 5.38788e-315

1 0 5.39306e-315

1 1 5.39824e-315

1 2 5.40342e-315

1 3 5.4086e-315

1 4 5.41378e-315

1 5 5.41896e-315

1 6 5.41896e-315

1 7 5.41896e-315

So the velocity values in the matrix are wrong. Any ideas?

Thanks,

D.Martelli September 15, 2011 08:57

fscanf problem
 
Hi i had got the same problem and i soved it defining the variable as float and not double...try it and let me know
by daniele:confused:

Alimohamadi_nasr March 11, 2013 06:18

Quote:

Originally Posted by D.Martelli (Post 324240)
Hi i had got the same problem and i soved it defining the variable as float and not double...try it and let me know
by daniele:confused:

dear D.Martelli many thanks, it also works for me.:rolleyes:

ghost82 June 4, 2014 07:17

I got crazy with this fscanf....thanks Martelli, declaring the variable as float works!

Daniele

eneja June 24, 2014 11:58

Where do you use this code? I thought UDF needs DEFINE macro...

ghost82 June 24, 2014 18:01

Hi, in a few words,
fscanf is a C function to scan a file for its content.
Macros are written in C language to communicate with fluent.
Inside macros you use C language, so you can use the fscanf function.

Daniele

eneja June 26, 2014 03:53

Thank you for your explanation, but in the code from above, there is no macro, that would communicate with fluent. I assume that part is left out, or is it possible to execute code as it is?

Thanks

sescobar July 7, 2014 12:19

I was having the same problem. Changing variable from real or double to float worked perfectly.

Thanks

myaghoobi2 September 19, 2017 03:33

fscanf in udf
 
Hi

Changing the double or real to float will work I think because we have used "%g" in fscanf function

Dilz April 6, 2024 16:30

Thanks a lot guys. This works !!


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