Thread: reformatting fields

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    reformatting fields

    version gcc 2.95.3
    SunOS 5.6

    Hi,

    I'm a bit stuck, I have to open a text file, read records and write them out to a new text file. The records are delimited by a semi-colon, there are between 80 and 90 fields, and about 40/50,000 records per file per month. A little less than half of them have to be formatted slightly differently, for example

    the fifth field can look like this

    ;1724470.00;
    ;774375.00;
    ;17022.00;
    ;3824571.00;

    and needs to look like an integer(20)

    ;1724470;
    ;774375;
    ;17022;
    ;3824571;

    Should I write out each field in a seperate fprintf statement ?


    tia,

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Should I write out each field in a seperate fprintf statement ?
    Either way is fine, but you may find that performance is affected when you run large amounts of data. Design your program the way you feel most comfortable with, then if it's too slow, start the fine tuning process. Most times you'll find that a program runs quick enough, and spending hours of coding time to save 2 seconds of runtime is a waste (not all the time though ).


    >>and needs to look like an integer(20)
    Remember, you don't need to convert the input from a char array to an in to do that. You are simply ending the string at the decimal point. This *may* save you some time, depending on how you're processing the line (ie how you read it in).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    thanks Hammer

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    don't know if you can help me with this,

    Code:
    void populate_output_record(RecordIn * input, RecordOut * output)
    {
    #ifdef DEBUG
        printf("In populate_output_record().\n");
        fflush(NULL);
    #endif
    
       memset(output, SPACE_INT, sizeof(RecordOut));
    
       memcpy(output->country,                  input->country,                  I_COUNTRY_LEN);
       memcpy(output->region_code,              input->region_code,              I_REGION_CODE_LEN);
       memcpy(output->region_name,              input->region_name,              I_REGION_NAME_LEN);
       memcpy(output->country_iso3_code,        input->country_iso3_code,        I_COUNTRY_ISO3_CODE_LEN];
       memcpy(output->country_iso2_code,        input->country_iso2_code,        I_COUNTRY_ISO2_CODE_LEN];
       memcpy(output->entity_code,              input->entity_code,              I_ENTITY_CODE_LEN];
       memcpy(output->entity_name,              input->entity_name,              I_ENTITY_NAME_LEN];
    
       memcpy(output->cirprecno,                fprint(input, "%d", cirprecno),                I_CIRPRECNO_LEN];
    
       memcpy(output->customer_defined_code,    input->customer_defined_code     I_CUSTOMER_DEFINED_CODE_LEN];
       memcpy(output->service_order_ref,        input->service_order_ref,        I_SERVICE_ORDER_REF_LEN];
    
    etc.,
    cirprecno is the field thats looks like 1724470.00
    and needs to look like 1724470

    but I cannot workout how to format it while processing


    tia,

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can't really use memcpy for this, as the length of the fields varies. For example, country names are different length, so copying a fixed amount of data won't work. I am presuming your input isn't padded with blanks to fixed field lengths, that would be kinda pointless in a comma seperated file.

    You might find this of use:
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    Also, this might give you some clues:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char a[] = "blah;one;two";
      char *p1, *p2;
      
      p1 = strchr(a, ';');
      if (p1)
      {
        p1++;
        puts (p1);
        p2 = strchr(p1, ';');
        if (p2)
        {
          printf ("%.*s\n", p2 - p1, p1);
        }
      }
        
      return(0);
    }
    
    /* Output:
      
      ;one;two
      one
      */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    aah -

    the penny drops



    thanks Hammer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 02:38 PM
  2. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  4. strings & fields
    By marko in forum C Programming
    Replies: 10
    Last Post: 05-01-2003, 01:42 PM