C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2005, 03:51 AM   #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;
    }
}
shoobsie is offline   Reply With Quote
Old 06-30-2005, 03:59 AM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?

Last edited by quzah; 06-30-2005 at 04:04 AM.
quzah is offline   Reply With Quote
Old 06-30-2005, 04:20 AM   #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!!
shoobsie is offline   Reply With Quote
Old 06-30-2005, 04:51 AM   #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
mitakeet is offline   Reply With Quote
Old 06-30-2005, 07:19 AM   #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;
}
shoobsie is offline   Reply With Quote
Old 06-30-2005, 07:38 AM   #6
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
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.
kermit is offline   Reply With Quote
Old 06-30-2005, 08:03 AM   #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
mitakeet is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22