Help debugging my program
first of all, let me thank anyone who has helped me over the past week. a week ago, i knew NOTHING about C programming and today i´ve been able to construct the program below.
hence the reason i´ve made alot of stupid errors.
my project was to read some data and manipulate it so that only some of it is outputed into a file.
the file created is in a folder PRN>>YEAR>>DAY>>hour.
all this data is extracted from the initial data coming in. i havent been able to take the time info from the data so i just made some numbers up.
the program was created in three parts = folder creation, string manipulation and writing and finally string parsing.
the output should be a file with the initial data, parsed and written into the approriate hour file.
there are no errors but the program does silly things.
please remember, i didnt know C programming until a week ago so be gentle!!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
//DATA COMING IN
char streaminput[1000] = "#RAWWAASFRAMEA,COM1,9,68.0,SATTIME,1263,126458.000 ,00000000,58e4,1522,22,103,62,5663456345dba436,22*d04567cd e";
char wasteitems[600] = "#RAWWAASFRAMEA SATTIME COM ; , * .";
//WORKS WITH SOME ERRORS!!!
//output array of filtering stage
char decoddate[9] = "20 06 05"; // ONLY FOR TEST!!!
void main()
{
//char *filteredstream;
char *firstfilter;
char *secondfilter;
char *thirdfilter;
char *fourthfilter;
char *decodweek;
char *decodtime;
char *seventhfilter;
char *eigthfilter;
char *ninthfilter;
char *tenthfilter;
char *eleventhfilter;
char *decodPRN;
char *decodmsgtype;
char *decodmsg;
char *fifteenthfilter;
char *decodPRC;
int PRN;
printf( "remaining data:\n" );
firstfilter = strtok( streaminput, wasteitems );
//****************Opens a file in the temp folder to write temp array to************//
secondfilter = strtok( NULL, wasteitems );
thirdfilter = strtok( NULL, wasteitems );
fourthfilter = strtok( NULL, wasteitems );
decodweek = strtok( NULL, wasteitems );
printf( "\n decodweek: %s", decodweek );
decodtime = strtok( NULL, wasteitems );
printf( "\n decodtime: %s", decodtime );
seventhfilter = strtok( NULL, wasteitems );
eigthfilter = strtok( NULL, wasteitems );
ninthfilter = strtok( NULL, wasteitems );
tenthfilter = strtok( NULL, wasteitems );
eleventhfilter = strtok( NULL, wasteitems );
decodPRN = strtok( NULL, wasteitems );
printf( "\n decodPRN: %s", decodPRN );
decodmsgtype = strtok( NULL, wasteitems );
printf( "\n decodmsgtype: %s", decodmsgtype );
decodmsg = strtok( NULL, wasteitems );
printf( "\n decodmsg: %s", decodmsg );
fifteenthfilter = strtok( NULL, wasteitems );
decodPRC = strtok( NULL, wasteitems );
printf( "\n decodPRC: %s \n\n", decodPRC );
//conversion from string to int for the folder names*******
PRN = atoi(decodPRN);
//intPRN = strtol(decodPRN,NULL, 10);
//printf("PRN: %3d", PRN);
//day = strtol(decodday,NULL, 10);
//printf("intday: %3d", day);
//year = strtol(decodyear,NULL, 10);
//printf("intyear: %3d", day);
//hour = strtol(decodhour,NULL, 10);
//printf("inthour: %3d", hour);
char buffPRN[20]; //string for PRN name
char buffyear[20]; //string for year
char buffday[20]; //string for day
char buffhour[20];
int hour = 2; //*********FOR TEST PURPOSE************
int day = 10; //*********REMOVE BEFORE USE***********
int year = 2005;//*************************************
//******Checks to see if PRN exists and creates new directory if statement if false*********\\
sprintf(buffPRN, "c:\\temp\\PRN%3d", PRN);
SetCurrentDirectory(buffPRN); //sets directory to current PRN
if(SetCurrentDirectory(buffPRN) == NULL)
{ CreateDirectory(buffPRN, NULL);
printf("\n\n\nNew PRN detected and folder created\n\n\n");
}else
//******Checks to see if year exists and creates new directory if statement if false********\\
sprintf(buffyear, "c:\\temp\\PRN%3d\\Y%4d", PRN, year);
SetCurrentDirectory(buffyear); //sets directory to current year
if (SetCurrentDirectory(buffyear) == NULL)
{ CreateDirectory(buffyear, NULL);
printf("\n\n\nNew Year detected and folder created\n\n\n");
}else
//******Checks to see if day exists and creates new directory if statement if false*********\\
sprintf(buffday, "c:\\temp\\PRN%3d\\Y%4d\\d%3d", PRN, year, day);
SetCurrentDirectory(buffday); //sets directory to current day
if (SetCurrentDirectory(buffday) == NULL)
{ CreateDirectory(buffday, NULL);
printf("\n\n\nNew Day detected and folder created\n\n\n");
}else
//***************Creates new hourly file if none exists and opens for writing***************\\
sprintf(buffhour, "c:\\temp\\PRN%d\\Y%d\\d%d\\h%d.txt", PRN, year, day, hour);
//constructing the final data to be written
char decoded[1];
strcat( decoded, decodPRN );
strcat( decoded, " " );
strcat( decoded, decoddate );
strcat( decoded, " " );
strcat( decoded, decodtime );
strcat( decoded, " " );
strcat( decoded, decodmsgtype );
strcat( decoded, " " );
strcat( decoded, decodmsg );
strcat( decoded, " " );
strcat( decoded, decodPRC );
printf( "String = %s\n", decoded );
{
//*************This portion writes the formatted data to the appropriate ems file************//
FILE *fp = fopen(buffhour, "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 );
printf("\n\n\ndata written to file\n\n");
//******************Closes the file and waits for next cycle*************************/
fclose ( fp );
fflush(NULL);
}
}}