output_file
Sets the output file or the error message output file.
Synopsis with Optional Arguments
#include <imsl.h>
void imsl_output_file (
IMSL_SET_OUTPUT_FILE, FILE *ofile,
IMSL_GET_OUTPUT_FILE, FILE **pofile,
IMSL_SET_ERROR_FILE, FILE *efile,
IMSL_GET_ERROR_FILE, FILE **pefile,
0)
Optional Arguments
IMSL_SET_OUTPUT_FILE, FILE *ofile (Input)
Set the output file to ofile.
Default: ofile = stdout
IMSL_GET_OUTPUT_FILE, FILE **pfile (Output)
Set the FILE pointed to by pfile to the current output file.
IMSL_SET_ERROR_FILE, FILE *efile (Input)
Set the error message output file to efile.
Default: efile = stderr
IMSL_GET_ERROR_FILE, FILE **pefile (Output)
Set the FILE pointed to by pefile to the error message output file.
Description
This function allows the file used for printing by IMSL routines to be changed.
If multiple threads are used then default settings are valid for each thread. When using threads it is possible to set different output files for each thread by calling imsl_output_file in Chapter 15 of the IMSL Stat Numerical Libraries from within each thread. See Example 2 for details.
Examples
This example opens the file myfile and changes the output file to this new file. The function imsl_f_write_matrix then writes to this file.
#include <stdio.h>
#include <imsl.h>
extern FILE* imsl_fopen(char* filename, char* mode);
extern int imsl_fclose(FILE* file);
int main()
{
FILE *ofile;
float x[] = {3.0, 2.0, 1.0};
imsl_f_write_matrix ("x (default file)", 1, 3, x, 0);
ofile = imsl_fopen("myfile", "w");
imsl_output_file(
IMSL_SET_OUTPUT_FILE, ofile,
0);
imsl_f_write_matrix ("x (myfile)", 1, 3, x, 0);
imsl_fclose(ofile);
}
Output
x (default file)
1 2 3
3 2 1
File myfile
x (myfile)
1 2 3
3 2 1
This example illustrates how to direct output from IMSL routines that run in separate threads to different files. First, two threads are created, each calling a different IMSL function, then the results are printed by calling imsl_f_write_matrix from within each thread. Note that imsl_output_file is called from within each thread to change the default output file.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <imsl.h>
void *ex1(void* arg);
void *ex2(void* arg);
extern FILE* imsl_fopen(char* filename, char* mode);
extern int imsl_fclose(FILE* file);
int main()
{
pthread_t thread1;
pthread_t thread2;
/* Create two threads. */
if (pthread_create(&thread1, NULL ,ex1, (void *)NULL) != 0)
perror("pthread_create"), exit(1);
if (pthread_create(&thread2, NULL ,ex2, (void *)NULL) != 0)
perror("pthread_create"), exit(1);
/* Wait for threads to finish. */
if (pthread_join(thread1, NULL) != 0)
perror("pthread_join"),exit(1);
if (pthread_join(thread2, NULL) != 0)
perror("pthread_join"),exit(1);
}
void *ex1(void *arg)
{
float *rand_nums = NULL;
FILE *file_ptr;
/* Open a file to write the result in. */
file_ptr = imsl_fopen("ex1.out", "w");
/* Set the output file for this thread. */
imsl_output_file(
IMSL_SET_OUTPUT_FILE, file_ptr,
0);
/* Compute 5 random numbers. */
imsl_random_seed_set(12345);
rand_nums = imsl_f_random_uniform(5, 0);
/* Output random numbers. */
imsl_f_write_matrix("Random Numbers", 5, 1, rand_nums, 0);
if (rand_nums) imsl_free(rand_nums);
imsl_fclose(file_ptr);
}
void *ex2(void *arg)
{
int n = 3;
float *x;
float a[] = {1.0, 3.0, 3.0, 1.0, 3.0, 4.0, 1.0, 4.0, 3.0};
float b[] = {1.0, 4.0, -1.0};
FILE *file_ptr;
/* Open a file to write the result in. */
file_ptr = imsl_fopen("ex2.out", "w");
/* Set the output file for this thread. */
imsl_output_file(
IMSL_SET_OUTPUT_FILE, file_ptr,
0);
/* Solve Ax = b for x */
x = imsl_f_lin_sol_gen (n, a, b, 0);
/* Print x */
imsl_f_write_matrix ("Solution, x, of Ax = b", 1, 3, x, 0);
if(x) imsl_free(x);
imsl_fclose(file_ptr);
}
Output
The content of the file ex1.out is shown below.
Random Numbers
1 0.0966
2 0.8340
3 0.9477
4 0.0359
5 0.0115
Output
The content of the file ex2.out is shown below.
Solution, x, of Ax = b
1 2 3
-2 -2 3