📄 showdata.c
字号:
#include "showdata.h"#include "main.h"#include "math.h"#include "rint.h"#include <stdlib.h>#include <math.h>/*************************************************************************** ** ROUTINE* ShowData** FUNCTION* Save data to disk in a format that can be later used* by graphical or other programs. Since the user can* specify how to save the data, this routine can be used* for saving data for many different uses. Adding new* formats is easily accomplished by altering the * showdata.h file and adding new switches and routines* to this file.** SYNOPSIS* ShowData(which, name, what, what_kind, how_many)** formal* data I/O* name type type function* -------------------------------------------------------------------* which FILE i/o UNIX file pointer * name char i Name of file to write to* what void i The data to save* what_kind int i The type of data to save* how_many int i The amount of data to save***************************************************************************/void ShowData(FILE **which, /* File pointer */char name[80], /* Name of file on disk */void *what, /* What data to save */int what_kind, /* What kind of data to save */int how_many) /* Number of elements of data to save */{int valid;short *new_short;/* Insure that that the file pointer is open */ if(*which == NULL) { *which = fopen(name, "w"); if(*which == NULL) { printf("Error opening %s\n", name); valid = FALSE; } else { valid = TRUE; } } else valid = TRUE;/* Switch to correct method of saving */ if(valid) { switch(what_kind) { case FLOAT : /* writes floating point data to disk file */ save_float((float *)what, how_many, *which, name); break; case INT: /* writes integer data to disk file */ save_int((int *)what, how_many, *which, name); break; case SHORT: /* writes short data to disk file */ save_short((short *)what, how_many, *which, name); break; case FLOAT2SHORT: /* converts floating point data to short and writes to disk file */ new_short = (short *) calloc(how_many, sizeof(short)); float2short((float *)what, how_many, new_short); save_short(new_short, how_many, *which, name); free(new_short); break; case FLOAT2ASCII: /* converts floating point data to ASCII (%1.3f) and writes to disk file */ float2ascii(how_many, *which, (float *)what); break; case INT2ASCII: /* converts integer data to ASCII (%d) and writes to disk file */ int2ascii(how_many, *which, (int *)what); break; default: printf("\n***** Invalid type sent to ShowData in showdata.c ... exiting *****\n"); exit(1); break; } } }/*************************************************************************** ** ROUTINE* save_float** FUNCTION* Save data to disk in floating point format * SYNOPSIS* save_float(what, how_many, which, name)** formal* data I/O* name type type function* -------------------------------------------------------------------* what float i The data to save* how_many int i The amount of data to save* which FILE i/o UNIX file pointer * name char i Name of file to write to***************************************************************************/void save_float(float what[],int how_many,FILE *which,char name[80]){int num_written;/* Write floating point data to a valid file */ num_written = fwrite(what, sizeof(what[0]), how_many, which); if (num_written != how_many) printf("Error: only %d samples written to %s\n", num_written, name);}/*************************************************************************** ** ROUTINE* save_int** FUNCTION* Save data to disk in integer format * SYNOPSIS* save_int(what, how_many, which, name)** formal* data I/O* name type type function* -------------------------------------------------------------------* what int i The data to save* how_many int i The amount of data to save* which FILE i/o UNIX file pointer * name char i Name of file to write to***************************************************************************/void save_int(int what[],int how_many,FILE *which,char name[80]){int num_written;/* write integer data to a valid file */ num_written = fwrite(what, sizeof(what[0]), how_many, which); if (num_written != how_many) printf("Error: only %d samples written to %s\n", num_written, name);}/*************************************************************************** ** ROUTINE* save_short** FUNCTION* Save data to disk in short integer format * SYNOPSIS* save_short(what, how_many, which, name)** formal* data I/O* name type type function* -------------------------------------------------------------------* what short i The data to save* how_many int i The amount of data to save* which FILE i/o UNIX file pointer * name char i Name of file to write to***************************************************************************/void save_short(short what[],int how_many,FILE *which,char name[80]){int num_written;/* write short data to a valid file */ num_written = fwrite(what, sizeof(what[0]), how_many, which); if (num_written != how_many) printf("Error: only %d samples written to %s\n", num_written, name);}/*************************************************************************** ** ROUTINE* float2short** FUNCTION* Convert data to short integer format from* floating point data.** SYNOPSIS* float2short(what, how_many, new_short)** formal* data I/O* name type type function* -------------------------------------------------------------------* what float i The data to convert* how_many int i The amount of data to save* new_short short o The converted data***************************************************************************/void float2short(float what[],int how_many,short new_short[]){int i;/* assign float numbers to short array */ for(i=0;i<how_many;i++) { new_short[i] = irint(max(min(what[i], 32767), -32768)); }}/*************************************************************************** ** ROUTINE* float2ascii** FUNCTION* Convert data to ascii format from* floating point data and write to a file.** SYNOPSIS* float2ascii(num, which, what)** formal* data I/O* name type type function* -------------------------------------------------------------------* num int i The amount of data to save* which FILE i The file point to write to* what float i The data to convert***************************************************************************/void float2ascii(int num,FILE *which,float what[]){int i; for(i=0;i<num;i++) { fprintf(which, "%1.3f ", what[i]); } fprintf(which,"\n");}/*************************************************************************** ** ROUTINE* int2ascii** FUNCTION* Convert data to ascii format from* integer data and write to a file.** SYNOPSIS* int2ascii(num, which, what)** formal* data I/O* name type type function* -------------------------------------------------------------------* num int i The amount of data to save* which FILE i The file point to write to* what int i The data to convert***************************************************************************/void int2ascii(int num,FILE *which,int what[]){int i; for(i=0;i<num;i++) { fprintf(which, "%d ", what[i]); } fprintf(which,"\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -