display file objects in different format

This is a discussion on display file objects in different format within the C Programming forums, part of the General Programming Boards category; The file that I have has the following format of the customers :- Customer file format: <id>,<surname>,<firstname>,<address>,<suburb>,<pos tcode>, <phone> Example: ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    display file objects in different format

    The file that I have has the following format of the customers :-

    Customer file format:
    <id>,<surname>,<firstname>,<address>,<suburb>,<pos tcode>,
    <phone>


    Example:
    C0001,Bloggs,Joe,123 Long Street,Melbourne,3000,93005555

    I could display them in the same format but I want to display them as follewing:-

    Example:

    ID Surname FirstName Address Suburb Post Phone
    ----- ------------ -------- -------------------- ------------ ---- -------- C0001 Bloggs Joe 123 Long Street Melbourne 3000 93005555
    ... ... ... ... ... ... ...


    I have written some of the structures for this:
    Code:
    /* Constants for customer information. */
    #define CUSTID_LEN 5
    #define SURNAME_MAX 12
    #define FIRSTNAME_MAX 12
    #define ADDRESS_MAX 20
    #define SUBURB_MAX 12
    #define POSTCODE_LEN 4
    #define PHONENUM_LEN 8
    
    typedef struct customerNode* CustomerNodePtr;
    typedef struct stockNode* StockNodePtr;
    
    /* Structure definitions. */
    typedef struct customerNode
    {
       char custID[CUSTID_LEN + 1];
       char surname[SURNAME_MAX + 1];
       char firstName[FIRSTNAME_MAX + 1];
       char address[ADDRESS_MAX + 1];
       char suburb[SUBURB_MAX + 1];
       unsigned postCode;
       unsigned phoneNum;
       CustomerNodePtr nextCust;
    } CustomerNodeType;
    I would really appreciate anyone who helps me with this one.
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    ID Surname FirstName Address Suburb Post PhoneNum
    ---- ----------- ------------ -------------------- ------------ ------ ---------------
    C0001 Bloggs Joe 123 Long Street Melbourne 3000 93005555


    It is the above format. Thanks

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Do you have a specific question? We're not going to write the program for you. If you don't know what to do next - just take the process one step at a time. Read the data in from the file, extract the actual values, rewrite them in your own format.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Code:
    #include<stdio.h>
    #include"ts.h"
    
    int main(void)
    
    {
    
    FILE *fp;
    
    char c;
    fp = fopen("customer.csv", "r");
    do
    putchar(c = getc(fp));
    while(c != EOF);
    return EXIT_SUCCESS;
    }
    The above code displays all of the file as it is but how could you display it in the format above..

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you think on that, and get back to us. Oh, and your sample code is wrong. Read the EOF faq for one of the reasons it's wrong.


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

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    If i could think on this man then I could do it myself, isn't it and you are saying that my code is wrong but it works well on my computer. Just check it again.
    Thanks

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You just check it again. Or better yet, read the EOF FAQ. EOF is not a valid char. Also, you haven't included the correct header file for EXIT_SUCCESS. Contrary to popular belief, I actually do know what I'm talking about most of the time.


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

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    Use fgets() to read each line
    Use strtok() to find each "," and strcpy() to copy each token to a member of your struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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