/* * ========================================================================= * filetest.c -- basic file I/O example. * * Open a specified input file (from command line, or request it) and read * first two lines. The first line is a comment line specifying what is in * the file (or what the data represents). The second line is the count of * the number of remaining lines of data. For this example, the data in the * file will be regarded as real and imaginary part of a complex number and * converted to magnitude and angle. The converted data will be output to * a new file. * * Written By : D. Mazzoni and M. Austin January 1994 * ========================================================================= */ #include #include /* for calloc declaration */ #include /* for strcpy */ #include "miscellaneous.h" /* miscellaneous prototypes and enums */ #include "convertcomplex.h" /* type definitions and prototype to process data */ int main( int argc, char ** argv ) { FILE * FpIn, * FpOut; /* File handles */ char caInFile[MaxFile]; /* input filename */ char caOutFile[MaxFile]; /* output filename */ char caErrorMessage[MaxFile];/* buffer for error message */ char caComment[MaxChar]; /* comment line storage */ int iNumLines; /* number of input lines */ int iCnt; /* line counter */ COMPLEX * cxpInData; /* dynamically allocated after file size known */ EULER * eupOutData; /* dynamically allocated after file size known */ const char * const kcpkReadFile = "r"; /* file read/write constants */ const char * const kcpkWriteFile = "w"; const char * const kcpkIndicator = "converted"; /* indicate change in output * file comment line */ /* * [a] : Check if input and output files have been included on the command * line. If yes, open files entered from the command line, copy names * if from argv. If not, then prompt the user for the file names. */ if( argc != 3 ) { do { printf( "\n\nEnter the input filename -->" ); gets( caInFile ); FpIn = fopen( caInFile, kcpkReadFile ); CheckFile( FpIn ); } while( NULL == FpIn ); do { printf( "Enter output filename -->" ); gets( caOutFile ); FpOut = fopen( caOutFile, kcpkWriteFile ); CheckFile( FpOut ); } while( NULL == FpOut ); } else { FpIn = fopen( strcpy( caInFile, argv[1] ), kcpkReadFile ); FpOut = fopen( strcpy( caOutFile, argv[2] ), kcpkWriteFile ); if( NULL == FpIn || NULL == FpOut ) { sprintf( caErrorMessage, "Fatal Error: %s in %s on line %3i.", __FILE__, __LINE__ ); fatalError( caErrorMessage, NULL); } } /* [b] : Now that files are open, we can process data. First read and printf * the comment line to distinguish this data set. */ fgets( caComment, MaxChar, FpIn ); printf( "File \"%s\" contains: %s", caInFile, caComment ); /* [c] : Get the number of lines in the input file: */ if( 1 != fscanf( FpIn, "%i%*c", &iNumLines ) ) fatalError( "Bad or missing number of data points in input file", NULL ); /* [d] : Allocate memory for data */ cxpInData = (COMPLEX *) safeCalloc( iNumLines, sizeof( COMPLEX ), __FILE__, __LINE__ ); eupOutData = (EULER *) safeCalloc( iNumLines, sizeof( EULER ), __FILE__, __LINE__ ); /* [e] : Read the data into the array */ printf( "Reading %d lines of input data ...", iNumLines ); for( iCnt = 0; iCnt < iNumLines; ++iCnt ) if( EOF == fscanf( FpIn, "%f%f", &(cxpInData[iCnt].fReal) , &(cxpInData[iCnt].fImag) ) ) { sprintf( caErrorMessage, "Premature end of data in %s on line %3d", __FILE__, __LINE__ ); fatalError( caErrorMessage, NULL); } printf( " done.\n"); /* [f] : Process the data, convert complex numbers */ printf( "Converting data to Euler's form ..."); for( iCnt = 0; iCnt < iNumLines; ++iCnt ) eupOutData[iCnt] = complexToEuler( cxpInData[iCnt] ); printf( " done.\n"); /* [g] : Output normalized data */ printf( "Writing converted data to file \"%s\" ...", caOutFile ); fprintf( FpOut, "%s %s", kcpkIndicator, caComment ); fprintf( FpOut, "%d\n", iNumLines ); for( iCnt = 0; iCnt < iNumLines; ++iCnt ) fprintf(FpOut, "%-f \t %-f\n", eupOutData[iCnt].fMagn, eupOutData[iCnt].fAngle); printf( " done.\n" ); /* [h] : cleanup and close files */ free( cxpInData ); free( eupOutData ); fclose( FpIn ); fclose( FpOut ); printf( "Done. Exiting program.\n" ); }