Thread: Help debugging my program

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    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);
    
    }
    }}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    Well you know this should be returning an int right?

    2. Most of this is written in C++ - at least there's a lot of dependency on using a C++ compiler
    Such things as // comments and embedded declarations in the middle of statements.

    3. Buffer overflows abound
    char decoded[1];
    strcat( decoded, decodPRN );
    strcat( decoded, " " );
    strcat( decoded, decoddate );
    How many characters can you fit into a 1-character array?
    Also, since the array lacks an obvious \0, who knows where the strcat actually starts writing


    Other points, you indentation needs a lot of work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks alot salem, i´ll look into these points.

    hovever, when i put
    Code:
    decoded[1]
    to anything higher, then it gives me alot of junk in the data written....

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I suggest you look up how strtok() works, because you're using it all wrong with your wasteitems array.

    Code:
    int main() {  /* yes, main returns int - see the FAQ */
    char *firstfilter;
    char *secondfilter;
    char *thirdfilter;
    char *fourthfilter;
    
    
    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 );
    
    if ( firstfilter != NULL ) printf( "first=%s\n", firstfilter );
    if ( secondfilter != NULL ) printf ( "second=%s\n", secondfilter );
    // etc etc
    
    return 0;
    }
    Until that works, it's a waste of effort getting the rest of the code in place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    I managed to get the code working perfectly.

    thanks to all those who helped.

    p.s. you may use this code if you want since everyone else helped me to create it in the first place!!

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <string.h>
    
    void main()
    
    {
    //*****************************************SAMPLE STRING INPUT FROM COM PORT**********************
       char streaminput[] = "#RAWWAASFRAMEA,COM1,9,68.0,SATTIME,1268,126458.000,00000000,58e4,1522,22,133,62,566345634563462323c000,22*d4567cde";
    //************************************REMOVE THE DECLARED VALUE ABOVE BEFORE USE************
    
       double a, GPSsec, c;
       int i, GPSweek, k, l, m, decodPRN, decodmsgtype, p;
       char COMport[5], decodmsg[100], decodCRC[20];
       if ( sscanf(streaminput, "#RAWWAASFRAMEA,%4[^,],%d,%lf,SATTIME,%d,%lf,%d,%lf,%d,%d,%d,%d,%99[^,],%d*%19[^\n]",
                   COMport, &i, &a, &GPSweek, &GPSsec, &k, &c, &l, &m, &decodPRN, &decodmsgtype, decodmsg, &p, decodCRC) == 14 )
     
    
    //****************************************CHECK TO SEE THAT WE HAVE THE CORRECT VALUE****************************************
          printf("GPSsec =       %g\n", GPSsec);
          printf("GPSweek =    %d\ndecodPRN =     %d\ndecodmsgtype = %d\n" , GPSweek, decodPRN, decodmsgtype);
          printf("COMport =      %s\n", COMport);
          printf("decodmsg =     %s\n", decodmsg);
          printf("decodCRC =     %s\n", decodCRC);
    //************************************************REMOVE ALL ABOVE BEFORE USE************************************************
    	  
    char stringPRN[4];					//conversion from decodPRN for final string "decoded"
    char stringmsgtype[5];				//conversion from decodmsgtype for final string "decoded"
    char buffPRN[5];					//string for PRN **needed for naming for folders**
    char buffyear[20];					//string for year |
    char buffday[20];					//string for day  |  These will be split from GPS time conversion value " TIME"
    char buffhour[20];					//string for day  |
    
    char decoded[250] = "\0";			//FINAL OUTPUT STRING, TERMINATED BY \0
    	
    int hour = 2;							//*************************************
    int day = 10;							//*********FOR TEST PURPOSE************
    int year = 2004;						//*********REMOVE BEFORE USE***********
    char time[9] = "09 42 07";				//*************************************
    char date[12] = "05 07 05";			    //*************************************
    
    //************************************Checking and setting of directories******************************************** 
    
    sprintf(buffPRN, "c:\\temp\\PRN%3d", decodPRN);
    	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");
    					
    }
    //******Checks to see if year exists and creates new directory if statement if false*********\\ 
    
    sprintf(buffyear, "c:\\temp\\PRN%3d\\Y%4d", decodPRN, 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");			
    }
    
    //******Checks to see if day exists and creates new directory if statement if false*********\\ 
    
    sprintf(buffday, "c:\\temp\\PRN%3d\\Y%4d\\d%3d", decodPRN, 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");
    			
    }			
    
    sprintf(buffhour, "c:\\temp\\PRN%3d\\Y%4d\\d%3d\\h%2d.ems", decodPRN, year, day, hour);
    
    //*****************************Conversion of Ints to Chars for the final output string************************
    
    sprintf(stringPRN, "%d", decodPRN);
    sprintf(stringmsgtype, "%d", decodmsgtype);
    //sprintf(stringtime, "%d", time);       ***********  NOT NEEDED YET (activate before use)  ************
    
    //***************************Joining of all appropriate strings to construct final string************************
    
    	memset(decoded,0,250);
    	strcat(decoded, stringPRN);
    	strcat(decoded, " ");
        strcat(decoded, time);
    	strcat(decoded, " ");
        strcat(decoded, date);
    	strcat(decoded, " ");
        strcat(decoded, stringmsgtype);
       	strcat(decoded, " ");
        strcat(decoded, decodmsg);
    	strcat(decoded, " ");
    	strcat(decoded, decodCRC);
        printf("\n\nString = %s\n\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");			// write to a new line	
        fputs ( decoded, fp );
    	printf("String Written in file\n\n");
    
    //******************Closes the file, flushes all buffers and waits for next cycle*************************
    
        fclose ( fp );
    	fflush(NULL);
    	printf("All processes successful\n\n");
    }
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM