A number of standard input and output (I/O) functions are available
in C and in ANSYS Fluent. They are listed below. All of the functions
work on a specified file except for printf, which displays information that is specified in the argument of
the function. The format string argument is the same for printf, fprintf, and fscanf. Note that all of these standard C I/O functions
are supported by the interpreter, so you can use them in either interpreted
or compiled UDFs. For more information about standard I/O functions
in C, you should consult a reference guide (for example, [6]).
|
Common C I/O Functions | |
|
|
opens a file |
|
|
closes a file |
|
|
formatted print to the console |
|
|
formatted print to a file |
|
|
formatted read from a file |
Important: It is not possible to use the scanf C function in ANSYS Fluent.
Important: These standard C I/O functions cannot be used when using the
DEFINE_DPM_OUTPUT macro due to certain file operations that must be
performed by ANSYS Fluent. In this case, ANSYS Fluent will handle file opening
and closing. The macros par_fprintf and par_fprintf_head are provided for writing output to
the file. For additional details, refer to The par_fprintf_head and par_fprintf Macros and
DEFINE_DPM_OUTPUT
.
FILE *fopen(char *filename, char *mode);
The function fopen opens a file in
the mode that you specify. It takes two arguments: filename and mode. filename is a pointer to the file you want to open. mode is the mode in which you want the file opened. The options for mode are read "r", write "w", and append "a". Both arguments
must be enclosed in quotes. The function returns a pointer to the
file that is to be opened.
Before using fopen, you will first
need to define a local pointer of type FILE that is defined in stdio.h (for example, fp). Then, you can open the file using fopen, and assign it to the local pointer as shown below. Recall that stdio.h is included in the udf.h file,
so you do not have to include it in your function.
FILE *fp; /* define a local pointer fp of type FILE */
fp = fopen("data.txt","r"); /* open a file named data.txt in
read-only mode and assign it to fp */
int fclose(FILE *fp);
The function fclose closes a file that
is pointed to by the local pointer passed as an argument (for example, fp).
fclose(fp); /* close the file pointed to by fp */
int printf(char *format,...);
The function printf is a general-purpose
printing function that prints to the console in a format that you
specify. The first argument is the format string. It specifies how
the remaining arguments are to be displayed in the console. The format
string is defined within quotes. The values of the replacement variables
specified as arguments following the format string will be substituted
sequentially for each instance of %type in
the format string. The % character is used
to designate the format type. Some common format characters are: %d for integers, %f for floating
point numbers, and %e for floating point
numbers in exponential format (with e before
the exponent). The format string for printf is the same as for fprintf and fscanf.
In the example below, the text Content of variable
a is: will be displayed in the console, and the value
of the replacement variable, a, will be substituted
in the message for %d.
Example:
int a = 5;
printf("Content of variable a is: %d\n", a); /* \n denotes a new line */
Important: It is recommended that you use the ANSYS Fluent Message utility instead of printf for compiled
UDFs. See Message
for details
on the Message macro.
int fprintf(FILE *fp, char *format,...);
The function fprintf writes to a file
that is pointed to by fp, in a format that
you specify. The first argument is the format string. It specifies
how the remaining arguments are to be written to the file. The format
string for fprintf is the same as for printf and fscanf.
Example:
FILE *fp; fprintf(fp,"%12.4e %12.4e %5d\n",x_array[j][0], x_array[j][1], noface); int data1 = 64.25; int data2 = 97.33; fprintf(fp, "%4.2d %4.2d\n", data1, data2);
Important: Note that the standard C function fprintf cannot be used when using a DEFINE_DPM_OUTPUT macro because certain additional file operations must be handled
by ANSYS Fluent. For file writing in a DEFINE_DPM_OUTPUT macro the par_fprintf and par_fprintf_head macros must be used. For additional details, refer to The par_fprintf_head and par_fprintf Macros and
DEFINE_DPM_OUTPUT
.
int fscanf(FILE *fp, char *format,...);
The function fscanf reads from a file
that is pointed to by fp, in a format that
you specify. The first argument is the format string. It specifies
how the data that is to be read is to be interpreted. The replacement
variables that follow the format string are used to store values that
are read. The replacement variables are preceded by the & character. Note that the format string for fscanf is the same as for fprintf and printf.
In the example below, two floating point numbers are read from
the file pointed to by fp, and are stored
in the variables f1 and f2.
:
FILE *fp; fscanf(fp, "%f %f", &f1, &f2);
Important: You cannot use the scanf I/O function
in ANSYS Fluent. You must use fscanf instead.