Thread: getting data from file

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    66

    getting data from file

    I am not sure of the best wat to get my data from a file and convert it into it correct data type.

    I have used fgets(); to get the line of data but I am not sure of the best way of converting it the data can be of diffrent types which is determined by the first character. My problem is that the string then wants breaking down in to various fields
    i.e. one of the types is as follows :
    the record type ( 1 character )
    the customer code (5 characters ) convert to numeric
    the customer name ( upto 20 characters ) may contaon spaces
    the customer address( upto 60 characters) seperated by 4 ; and may contain spaces
    the Customer balance( 9 characters) to become floating point
    and the Credit limit ( 7 characters) convert to numeric

    what is the best way to disect the string into the individual elements


    Thanx in advance
    T

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    have you looked at strtok?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    good point i didnt notice the "may contaon spaces" part till now. if it the programs file and not something the user has to deal with he could come up with something unique to be a delimiter. just a n00b's idea..........

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: getting data from file

    Originally posted by task
    I am not sure of the best wat to get my data from a file and convert it into it correct data type.

    I have used fgets(); to get the line of data but I am not sure of the best way of converting it the data can be of diffrent types which is determined by the first character. My problem is that the string then wants breaking down in to various fields
    i.e. one of the types is as follows :
    the record type ( 1 character )
    the customer code (5 characters ) convert to numeric
    the customer name ( upto 20 characters ) may contaon spaces
    the customer address( upto 60 characters) seperated by 4 ; and may contain spaces
    the Customer balance( 9 characters) to become floating point
    and the Credit limit ( 7 characters) convert to numeric

    what is the best way to disect the string into the individual elements


    Thanx in advance
    Create a structure that defines the format of the line. But keep in mind the Customer balance is 9 characters and not a string because it is not null-terminated. You'll have to convert the values by hand.

    If the first character defines the format of the rest of the line, you may want to use a union -- it can use multiple structures to define the same input space.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    What's the delimiter between name and address?

    It is part of my coursework and I have read through it and it doesn't say, I am assuming that if the name occupies less than 20 characters the rest will just be whitespaces.
    T

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    is there anything saying your data file cant look like this

    "name","address"

    or even

    <name><address> ?

    that would give you something unique and if its just the data file the user wouldnt have to see it. you could just strtok for " or <>s and then run threw what it gives you. there might be a better way, but this should be simple.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    I don't produce the data the college does so I cant incorporate anthing into the data. so the data must be stored in the file as 1character for type than 20 characters for the name ( i.e. if name is only 10 characters it is followed by 10 spaces) to fill the 20 characters allowed and then the address starts and so on.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    yes that is as I understand it

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by task
    yes that is as I understand it
    Ok then, read 20 bytes and stick it in the name block, then continue from there to read whatever is next.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    I am not sure of how to do this as both the name and address may contain spaces i.e 123 street not 123_street or something like that. I dont want it to stop reading the data when it encounters a white space but other than using a function that only picks up one character each time it is called I am not sure how to do it.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read the complete line into one temporary buffer using fgets(), then pluck out the bits you want. If the input data is properly structured, and you're not worried about validation, it should be quite easy to do.

    Start by creating a struct to hold your data in, then learn how to populate it bit by bit. When you have some code together, post it here and someone will help you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    Code:
    int make_fields( char *string_ptr, RECORD *data_ptr )
    {
       if( sscanf( string_ptr, "%c%5s%21s%61s%10s%8s", 
                                  &data_ptr->Cre.Record_Type,
    	              data_ptr->Cre.Customer_Code,
    	              data_ptr->Cre.Customer_Name,
    	              data_ptr->Cre.Customer_Address,
                                  data_ptr->Cre.Customer_Balance,
    	              data_ptr->Cre.Credit_Limit) != 6 )
    		
            return ERROR;
       else
            return VALID;
    }
    The function is passed the address of the temporary string that took the line of data from the file using fgets() and RECORD *data_ptr points to the union that contains the various structures I require including the (Cre )one that I need for the example.

    am I close on this or a million miles off?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Looks fine, other than the possible problem with sscanf and your variables. If any of those are not character arrays, then you'll need to modify the call to use the address of said variable. Consider:
    Code:
    struct foo
    {
        int bar;
    };
    
    struct foo f, *b;
    
    b = &f;
    
    scanf("%d", &b->bar );
    You need the address symbol here to scan into the integer itself. You may be aware of this, but this is the only potential problem I can see, since you didn't provide the structure definition for us to look at.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Looks ok at a glance. Why not write a smaller main() to invoke and test it, and see if it does what you want.

    [edit]Doh, beaten.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    Looks fine, other than the possible problem with sscanf and your variables. If any of those are not character arrays, then you'll need to modify the call to use the address of said variable. Consider:

    code:--------------------------------------------------------------------------------
    struct foo
    {
    int bar;
    };

    struct foo f, *b;

    b = &f;

    scanf("%d", &b->bar );

    I was just going to create copies of them in diffrent formats as I need to store them back in file afterwards.
    i.e.

    [code]

    int temp_balance;

    temp_balance = atol( data_ptr->Cre.Customer_Balance );

    /* Then validate it here */

    [\code]

    Is this o.k. or not good practice
    T

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM