![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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 (e.g., [ 6]).
Common C I/O Functions | |
fopen("filename", "mode"); | opens a file |
fclose(fp); | closes a file |
printf("format", ...); | formatted print to the console |
fprintf(fp, "format", ...); | formatted print to a file |
fscanf(fp, "format", ...); | formatted read from a file |
|
It is not possible to use the
scanf C function in
ANSYS FLUENT.
|
fopen
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 (e.g., 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 don't 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 */ |
fclose
int fclose(FILE *fp);
The function fclose closes a file that is pointed to by the local pointer passed as an argument (e.g., fp).
fclose(fp); /* close the file pointed to by fp */ |
printf
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 value of the replacement variables that follow the format string will be substituted in the display for all instances of %type. The % character is used to designate the character 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 all instances of %d.
Example:
int a = 5; printf("Content of variable a is: %d\n", a); /* \n denotes a new line */ |
|
(UNIX only) It is recommended that you use the
ANSYS FLUENT
Message utility instead of
printf for compiled UDFs. See Section
3.7 for details on the
Message macro.
|
fprintf
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); |
fscanf
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.
Example:
FILE *fp; fscanf(fp, "%f %f", &f1, &f2); |
|
You cannot use the
scanf I/O function in
ANSYS FLUENT. You must use
fscanf instead.
|