#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12

    two jobs at once ?


    Hi,

    The following routine reads a text file a character at a time to locate a field within a record [each separate line].

    The data in the field represents a date and this is checked with the current date when the process runs to establish which of three new files should receive the record.

    My problem is trying to capture a record and a character at the same time, so that the whole line is written to one of the files.
    Code:
    void get_activity_month(char * input_file, FILE ** p_fp)
    {
    char buffer[BUFSIZ];
    char activity_month[BUFSIZ];
    char current_month[BUFSIZ];
    int start_date;
    int end_date;
    size_t index = 0;
    int ch, fields = 0, records = 0;
    time_t curtime;
    struct tm *loctime;
    char current_record[BUFFER_SIZE];
    
    #ifdef DEBUG
        printf("In get_activity_month().\n");
        fflush(NULL);
    #endif
    
       /* Get the current time. */
       curtime = time(NULL);
    
       /* Convert it to local time representation. */
       loctime = localtime(&curtime);
    
       /* get the current date in decimal format */
       strftime(current_month, BUFSIZ, "%Y%m", loctime);
    
       if(p_fp[IN_FILE] != NULL)
       {
          for(;;)
          {
             /* read complete line here */
    /*
             fread(current_record, 1, BUFFER_SIZE, p_fp[IN_FILE]);
             rewind(p_fp[IN_FILE]);
    
    
             while(fgets(current_record, BUFFER_SIZE, p_fp[IN_FILE]))
             {
                if(*current_record == '\n')
                   break;
             }
    */
             /* start chugging through the file */
             ch = fgetc(p_fp[IN_FILE]);
             if(ch == EOF)
             {
                break;
             }
             else if(ch == SEPARATOR || ch == '\n')
             {
                buffer[index] = '\0';
    
                /* field 80 begins at the 79th separator */
                if(fields == 79 && *buffer == NULL)
                {
                   error_func("In get_activity_month().", "Error : Activity_Month has no data", SYS | FATAL);
                }
                else if(fields == 79)
                {
                   /* get the year and month from the file and create two new dates for testing */
                   strncpy(activity_month, buffer, 6);
                   start_date = atoi(current_month);
                   start_date = start_date - 100;
                   end_date = atoi(current_month);
                   end_date = end_date--;
    
    #ifdef DEBUG
        printf("record %d, field %d: \"%s\"", records, fields, buffer);
        printf("  activity m %s - current m %s", activity_month, current_month);
        printf("  start %d - end %d\n", start_date, end_date);
        fflush(NULL);
    #endif
    
                   if(activity_month == current_month)
                   {
                      /* current month records only */
                      printf("write to this month's file\n");
                      fprintf(p_fp[OUT_FILE], current_record);
                   }
                   else if(atoi(activity_month) > start_date && atoi(activity_month) < end_date)
                   {
                      /* previous twelve months - not including current month */
                      printf("write to previous years file\n");
                      fprintf(p_fp[YEAR_FILE], current_record);
                   }
                   else
                   {
                      /* everything else */
                      printf("write to history file\n");
                      fprintf(p_fp[HIST_FILE], current_record);
                   }
                }
    
                /* choose between fields and records */
                if(ch == '\n')
                {
                   ++records;
                   fields = 0;
                }
                else
                {
                   ++fields;
                }
                index = 0;
             }
             else
             {
                if(index < sizeof(buffer))
                {
                   buffer[index++] = (char)ch;
                }
             }
         }
       }
    #ifdef DEBUG
        printf("Out get_activity_month().\n");
        fflush(NULL);
    #endif
    }

    tia,
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    68
    Rep Power
    12
    Why not just read a complete record and then process it in memory. Once it's processed, write it back to file:
    Code:
    while (!done) {
      // Read a record
      // Find the field
      // Process the field
      // Write the record
    }
    The standard library helps immensely with parsing records. As an alternative, you can read the entire file into, say, a linked list, and then have everything in memory so that it's easy to parse if you need more than one record at a time.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    12
    aye, your probably right



    thanks Kung Foo Master

IMN logo majestic logo threadwatch logo seochat tools logo