Thread: program sections were working fine until it came to putting it together

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

    program sections were working fine until it came to putting it together

    i created a program which will read some data from an array, filter some out and copy the useful part (filtering stage). it will then use some of the info to create a directory with the name taken from the data. then it will dump the original data into a file in the directory create.

    the directory structure is PRN>>Year>>Day>>hour(file)

    howver, i have managed to successfully create the directories and filter the data in sperate files.

    when i came to putting it together it refuses to work.

    there are no errors but it wont do what its supposed to.

    so far, it creates the directories but doesnt dump the data

    can anyone give me a hand, please?

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <windows.h>
    #include <io.h>
    
    int main ()  
      
    {
    	char streaminput[BUFSIZ] =
            "#RAWWAASFRAMEA,COM1,9,68.0,SATTIME,1263,126458.000,00000000,58e4,1522,22,122,62,5663456345dba43623443efdaef3245345fea32534562663462323c000,22*d04567cde";
    
        char wasteitems[BUFSIZ] = "#RAWWAASFRAMEA SATTIME COM ; , * .";
    
    //	int PRN = 152;   //*************************************
        int hour = 4;    //*********FOR TEST PURPOSE************
        int day = 146;   //*********REMOVE BEFORE USE***********
        int year = 2008; //*************************************
    	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;
    
    	//These will be taken each time from the filtered stream input//
    //***********************************************************************
        char decodPRN[BUFSIZ];  //string for PRN name
        char decodyear[BUFSIZ]; //string for year
        char decodday[BUFSIZ];  //string for day
        char decodmsgtype[BUFSIZ]; //string for msgtype
        char decodmsg[BUFSIZ];	//string for msg
    //***********************************************************************
    
    	{
    
        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);
    	
    //*******************copy useful parts of masg and ingore rest******************//
        strcpy(decodPRN, fifthfilter);
        strcpy(decodday, secondfilter);
        //strcpy(uncodtime, sixthfilter);  //not needed yet**constant defined earlier//
        strcpy(decodmsgtype, thirteenthfilter);
        strcpy(decodmsg, fourteenthfilter);
        //strcpy(uncodCRC, sixteenthfilter); /not needed yet**constant defined earlier//
    
        //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(decodPRN, "c:\\temp\\PRN%3d", decodPRN);
        SetCurrentDirectory(decodPRN); //sets directory to current PRN
    
        if (SetCurrentDirectory(decodPRN) == NULL)
            printf("\n\n\nNew PRN detected and folder created\n\n\n");
    
        CreateDirectory(decodPRN, NULL);
    
    //******Checks to see if year exists and creates new directory if statement if false********\\ 
    
        sprintf(decodyear, "c:\\temp\\PRN%3d\\Y%4d", decodPRN, 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", decodPRN, 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);
        printf("String = %s\n", decoded);
    
        {
        FILE *fp = fopen(decodday, "a+b");
    
        if ((fp = fopen(decodday, "a+b")) == NULL)
            {
            printf("Cannot open file\n");
            fclose(fp);
    
            //*********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;
        }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *fp = fopen(decodday, "a+b");
    
        if ((fp = fopen(decodday, "a+b")) == NULL)
    Why are you calling fopen twice?

    But wait, it gets worse:
    Code:
    if ((fp = fopen(decodday, "a+b")) == NULL)
    {
            printf("Cannot open file\n");
            fclose(fp);
    Why are you closing a file that failed to open?

    But wait, it gets worse:
    Code:
    if ((fp = fopen(decodday, "a+b")) == NULL)
    {
            printf("Cannot open file\n");
            fclose(fp);
    
            //*********Writes contents of decoded final data to the appropriate hour file********/
    
            fprintf(fp, "\n"); // should write to a new line	
            fputs(decoded, fp);
    Why are you trying to write to a file that failed to open, that you just closed, even though it didn't open?

    But wait, it gets worse:
    Code:
    if ((fp = fopen(decodday, "a+b")) == NULL)
    {
            printf("Cannot open file\n");
            fclose(fp);
    
            //*********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*************************/
    
    
            //******************Closes the file and waits for next cycle*************************/
    
            fclose(fp);
            }
    Why are you closing a file that failed to open, that you closed even though the open failed, that you just wrote to, that you had just closed, even though the file failed to open?


    Quzah.
    Last edited by quzah; 06-30-2005 at 04:04 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    woohoooo!!!

    even though i hate you for making an ass of me, i thank you for finding the solution!!!

    the files writes perfectly but now my directory names are screwd up!!

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Post your fixed code.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    i´m aware that i need to use the atoi function to change the strings to integers which can then be inserted into the directory names. one problem is, how!?

    my latest code

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

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by shoobsie
    i´m aware that i need to use the atoi function to change the strings to integers which can then be inserted into the directory names. one problem is, how!?
    Instead of atoi(), you might consider checking into the strtol() function.

  7. #7
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Lets look at this block of code:

    Code:
            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);
    You are setting the current directory, then checking to see if it exists, then creating it anyway. You repeat this over and over again. Seems pretty dumb, eh? I suggest you start completely over again with a blank page and recode your program, test and debug THAT version, then post back if you still have problems.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert to ASCII program almost working
    By ggraz in forum C Programming
    Replies: 8
    Last Post: 09-17-2008, 06:54 PM
  2. Program not working
    By gillypie in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2007, 02:39 PM
  3. Program logic not working..
    By ronkane in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2002, 08:31 PM
  4. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM
  5. Why isn't my program working?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 02:10 PM