output_file
Sets the output file or the error message output file.
Synopsis with Optional Arguments
#include <imsls.h>
void imsls_output_file (
IMSLS_SET_OUTPUT_FILE, FILE *ofile,
IMSLS_GET_OUTPUT_FILE, FILE **pofile,
IMSLS_SET_ERROR_FILE, FILE *efile,
IMSLS_GET_ERROR_FILE, FILE **pefile,
0)
Optional Arguments
IMSLS_SET_OUTPUT_FILE, FILE *ofile (Input)
Sets the output file to ofile.
Default: ofile = stdout
IMSLS_GET_OUTPUT_FILE, FILE **pofile (Output)
Sets the FILE pointed to by pofile to the current output file.
IMSLS_SET_ERROR_FILE, FILE *efile (Input)
Sets the error message output file to efile.
Default: efile = stderr
IMSLS_GET_ERROR_FILE, FILE **pefile (Output)
Sets the FILE pointed to by pefile to the error message output file.
Description
This function allows the file used for printing by IMSL functions 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
imsls_output_file from within each thread. See
Example 2 for more details.
Examples
Example 1
This example opens the file myfile and sets the output file to this new file. Function imsls_f_write_matrix then writes to this file.
#include <imsls.h>
#include <stdio.h>
extern FILE* imsls_fopen(char* filename, char* mode);
extern int imsls_fclose(FILE* file);
int main()
{
FILE *ofile;
float x[] = {3.0, 2.0, 1.0};
imsls_f_write_matrix ("x (default file)", 1, 3, x,
0);
ofile = imsls_fopen("myfile", "w");
imsls_output_file(IMSLS_SET_OUTPUT_FILE, ofile,
0);
imsls_f_write_matrix ("x (myfile)", 1, 3, x,
0);
imsls_fclose(ofile);
}
Output
x (default file)
1 2 3
3 2 1
File myfile
x (myfile)
1 2 3
3 2 1
Example 2
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
imsls_f_write_matrix from within each thread. Note that
imsls_output_file is called from within each thread to change the default output file.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <imsls.h>
void *ex1(void* arg);
void *ex2(void* arg);
extern FILE* imsls_fopen(char* filename, char* mode);
extern int imsls_fclose(FILE* file);
int main()
{
pthread_t thread1;
pthread_t thread2;
/* Disable IMSL signal trapping. */
imsls_error_options(
IMSLS_SET_SIGNAL_TRAPPING, 0,
0);
/* 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 = imsls_fopen("ex1.out", "w");
/* Set the output file for this thread. */
imsls_output_file(
IMSLS_SET_OUTPUT_FILE, file_ptr,
0);
/* Compute 5 random numbers. */
imsls_random_seed_set(12345);
rand_nums = imsls_f_random_uniform(5,
0);
/* Output random numbers. */
imsls_f_write_matrix("Random Numbers", 5, 1, rand_nums,
0);
if (rand_nums)
imsls_free(rand_nums);
imsls_fclose(file_ptr);
}
void *ex2(void* arg)
{
int n_intervals=10;
int n_observations=30;
float *table;
float x[] = {
0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
1.89, 0.90, 2.05
};
FILE *file_ptr;
/* Open a file to write the result in. */
file_ptr = imsls_fopen("ex2.out", "w");
/* Set the output file for this thread. */
imsls_output_file(
IMSLS_SET_OUTPUT_FILE, file_ptr,
0);
table = imsls_f_table_oneway (n_observations, x, n_intervals,
0);
imsls_f_write_matrix("counts", 1, n_intervals, table,
0);
if (table)
imsls_free(table);
imsls_fclose(file_ptr);
}
Output
The content of the file ex1.out is shown below.
Random Numbers
1 0.4919
2 0.3909
3 0.2645
4 0.1814
5 0.7546
The content of the file ex2.out is shown below.
counts
1 2 3 4 5 6
4 8 5 5 3 1
7 8 9 10
3 0 0 1