|
[Sponsors] |
How to read a Value from Text File and store it in Variables |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
October 26, 2020, 03:45 |
How to read a Value from Text File and store it in Variables
|
#1 |
Member
Amirreza Niazmehr
Join Date: Nov 2018
Posts: 40
Rep Power: 8 |
Hi guys
This is my text file (Data.txt): rho_0 10 kp_0 8 Beta_kp 6 x_min 5 x_max 8 y_min 9 y_max 5 z_min 4 z_max 7 I want to read from this text file line by line and store each Value in the parameter at the same line. for example, for the first line store 10 in rho_0. I have written as below.At the next end I want to use x_min and x_max ... to calculate Volume. I mean Volume=(x_max-x_min)*(y_max-y_min)*(z_max-z_min). I have written this code (as below) in C and now I want to use it in my UDF. First I want to define the Macros and then use this code. I just want to know if my code works, if I first use and define the proper Macros. I mean,is the body of my code proper for using in UDF? Code:
#include <stdio.h> #include <math.h> #include <stdlib.h> int main() { double rho_0, kp_0, Beta_kp; double x_min, x_max ,y_min ,y_max ,z_min, z_max, V_plasmazone; char param_name[100]; FILE *inputfile; /*char inputpath[100]; char path [100] ; strcpy(inputpath, path); strcat(inputpath, "Data.txt"); inputfile = fopen(inputpath, "r"); if(inputfile) { ScanCheck(fscanf(inputfile, "%s", inputpath), 1, inputfile, "Data.txt", 1); strcat(path, inputpath); strcat(path, "/"); } else { Error("Error opening input.txt - file!"); }*/ inputfile =fopen ("Data.txt","r") ; if(inputfile == NULL) { perror("Unable to open file!"); exit (1); } fscanf(inputfile, "%s %lf\n", param_name, &rho_0); printf("%s %lf\n", param_name, rho_0); fscanf(inputfile, "%s %lf\n", param_name, &kp_0); printf("%s %lf\n", param_name, kp_0); fscanf(inputfile, "%s %lf\n", param_name, &Beta_kp); printf("%s %lf\n", param_name, Beta_kp); fscanf(inputfile, "%s %lf\n", param_name, &x_min); printf("%s %f\n", param_name, x_min); fscanf(inputfile, "%s %lf\n", param_name, &x_max); printf("%s %f\n", param_name, x_max) ; fscanf(inputfile, "%s %lf\n", param_name, &y_min); printf("%s %f\n", param_name, y_min); fscanf(inputfile, "%s %lf\n", param_name, &y_max); printf("%s %f\n", param_name, y_max); fscanf(inputfile, "%s %lf\n", param_name, &z_min); printf("%s %f\n", param_name, z_min); fscanf(inputfile, "%s %lf\n", param_name, &z_max); printf("%s %f\n", param_name, z_max); V_plasmazone = ( x_max - x_min )*( y_max - y_min )*( z_max - z_min ); printf( "V_plasmazone %lf\n", V_plasmazone ); fclose(inputfile); return 0; } |
|
October 26, 2020, 09:40 |
|
#2 |
Senior Member
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27 |
To use it in Fluent, you should not make it main(), but use a Fluent Macro. Probably EXECUTE_ON_DEMAND is appropriate here; you can then tell Fluent to execute this code whenever you want (look in help to find out how if you don't know yet).
|
|
October 29, 2020, 08:00 |
|
#3 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
I agree with Pakk, you do propably want to use execute on demand macro
read Ansys Fluent Customization manual for good examples
__________________
best regards ****************************** press LIKE if this message was helpful |
|
October 29, 2020, 10:24 |
|
#4 |
Member
Amirreza Niazmehr
Join Date: Nov 2018
Posts: 40
Rep Power: 8 |
Thanks alot guys for your help
|
|
February 16, 2024, 18:20 |
problem with read file
|
#5 |
New Member
amina
Join Date: Feb 2020
Posts: 9
Rep Power: 6 |
hi everyone ,
I want to know if my data file (containing 20,000 values) is being read by the udf. To do this, I've created a small program that reads the data from the data.txt file and prints it to another file (data1.txt). My program compiles well, the library is created and I've hooked my udf to execute on demand. The data.txt file contains 4 values: 0.2 0.2 0.1 0.1 results on file data1.txt are : 0.000 0.000 0.000 0.000 ????????? where is the problem???? this is my udf: #include "udf.h" double coef[4]; DEFINE_ON_DEMAND(lire) { FILE* rfile; FILE* rfile1; rfile = fopen("C:\\Codes\\data.txt", "r"); rfile1 = fopen("C:\\Codes\\data1.txt", "w"); int i; i = 0; for (i = 0; i <4; i++) { fscanf(rfile, "%f", &coef[i]); fprintf(rfile1, "%f", coef[i]); } fclose(rfile); fclose(rfile1); } |
|
April 5, 2024, 16:14 |
|
#6 |
New Member
Join Date: May 2023
Posts: 2
Rep Power: 0 |
Hey, I have the exact same problem. I get zeros when I compile and view the output. Did you resolve this?
Any help is appreciated, Thanks |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[swak4Foam] How to define boundary condition variables by using previosly defined variables? | pawlo | OpenFOAM Community Contributions | 8 | September 13, 2020 12:37 |
Shape optimization doesn't converge - Adjoint log file | JPBLourenco | SU2 Shape Design | 0 | December 11, 2017 09:41 |
Journal command for multiple case file read, and store data of static pressure along | Ash Kot | ANSYS | 3 | July 10, 2017 04:08 |
UDF macro to read the name of the case file | polaritus | Fluent UDF and Scheme Programming | 2 | March 23, 2015 05:59 |
Results saving in CFD | hawk | Main CFD Forum | 16 | July 21, 2005 21:51 |