Code:
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <io.h>
int main ()
{
char streaminput[1000] =
"#RAWWAASFRAMEA,COM1,9,68.0,SATTIME,1263,126458.000,00000000,58e4,1522,22,999,62,5663456345dba43623443efdaef3245345fea32534562663462323c000,22*d04567cde";
char wasteitems[37] = "#RAWWAASFRAMEA SATTIME COM ; , * .";
// int PRN = 152; //*************************************
int hour = 19 ; //*********FOR TEST PURPOSE************
int day = 555; //*********REMOVE BEFORE USE***********
int year = 2008; //*************************************
char dirPRN[3];
char decoded[1]; //*************************************
// char decodPRN[10] = "122"; //*********FOR TEST PURPOSE************
char decoddate[12] = " 26 06 05 "; //*************************************
char decodtime[12] = " 13 59 59 "; //*********REMOVE BEFORE USE***********
// char decodmsgtype[10] = " 69"; //*************************************
// char decodmsg[100] = " 9acasdacadad23523d"; //*************************************
char *firstfilter;
char *secondfilter;
char *thirdfilter;
char *fourthfilter;
char *fifthfilter;
char *sixthfilter;
char *seventhfilter;
char *eigthfilter;
char *ninthfilter;
char *tenthfilter;
char *eleventhfilter;
char *twelvthfilter;
char *thirteenthfilter;
char *fourteenthfilter;
char *fifteenthfilter;
char *sixteenthfilter;
//These will be taken each time from the filtered stream input//
//***********************************************************************
char decodPRN[3]; //string for PRN name
char decodyear[4]; //string for year
char decodday[3]; //string for day
char decodmsgtype[2]; //string for msgtype
char decodmsg[212]; //string for msg
char decodCRC[10]; //string for CRC
//***********************************************************************
{
printf("remaining data:\n");
firstfilter = strtok(streaminput, wasteitems);
//****************recursive filtering to seperate sections and assign strings************//
secondfilter = strtok(NULL, wasteitems);
thirdfilter = strtok(NULL, wasteitems);
fourthfilter = strtok(NULL, wasteitems);
fifthfilter = strtok(NULL, wasteitems);
sixthfilter = strtok(NULL, wasteitems);
seventhfilter = strtok(NULL, wasteitems);
eigthfilter = strtok(NULL, wasteitems);
ninthfilter = strtok(NULL, wasteitems);
tenthfilter = strtok(NULL, wasteitems);
eleventhfilter = strtok(NULL, wasteitems);
twelvthfilter = strtok(NULL, wasteitems);
thirteenthfilter = strtok(NULL, wasteitems);
fourteenthfilter = strtok(NULL, wasteitems);
fifteenthfilter = strtok(NULL, wasteitems);
sixteenthfilter = strtok(NULL, wasteitems);
//*******************copy useful parts of masg and ingore rest******************//
strcpy(decodPRN, twelvthfilter);
strcpy(decodday, secondfilter);
//strcpy(uncodtime, sixthfilter); //not needed yet**constant defined earlier//
strcpy(decodmsgtype, thirteenthfilter);
strcpy(decodmsg, fourteenthfilter);
strcpy(decodCRC, sixteenthfilter);
//sixteenthfilter = strtok( NULL, wasteitems ); //not needed yet**constant defined earlier//
//******Checks to see if PRN exists and creates new directory if statement if false*********\\
sprintf(dirPRN, "c:\\temp\\PRN%3d", decodPRN);
SetCurrentDirectory(dirPRN); //sets directory to current PRN
if (SetCurrentDirectory(dirPRN) == NULL)
printf("\n\n\nNew PRN detected and folder created\n\n\n");
CreateDirectory(dirPRN, NULL);
//******Checks to see if year exists and creates new directory if statement if false********\\
sprintf(decodyear, "c:\\temp\\PRN%3d\\Y%4d", dirPRN, year);
SetCurrentDirectory(decodyear); //sets directory to current year
if (SetCurrentDirectory(decodyear) == NULL)
printf("\n\n\nNew Year detected and folder created\n\n\n");
CreateDirectory(decodyear, NULL);
//******Checks to see if day exists and creates new directory if statement if false*********\\
sprintf(decodday, "c:\\temp\\PRN%3d\\Y%4d\\d%3d", dirPRN, year, day);
SetCurrentDirectory(decodday); //sets directory to current day
if (SetCurrentDirectory(decodday) == NULL)
printf("\n\n\nNew Day detected and folder created\n\n\n");
CreateDirectory(decodday, NULL);
//***************Creates new hourly file if none exists and opens for writing***************/
SetCurrentDirectory(decodday);
sprintf(decodday, "c:\\temp\\PRN%3d\\Y%4d\\d%3d\\h%2d.txt", decodPRN, year, day, hour);
}
//*************This portion writes the formatted data to the appropriate ems file************\\
strcat(decoded, decodPRN );
strcat(decoded, decoddate );
strcat(decoded, decodtime );
strcat(decoded, decodmsgtype );
strcat(decoded, decodmsg );
strcat(decoded, decodCRC );
printf("String = %s\n", decoded);
FILE *fp = fopen(decodday, "a+b");
//*********Writes contents of decoded final data to the appropriate hour file********/
fprintf(fp, "\n"); // should write to a new line
fputs(decoded, fp);
//******************Closes the file and waits for next cycle*************************/
fclose(fp);
return 0;
}