Like Tree4Likes

i want write contact records with linked list

This is a discussion on i want write contact records with linked list within the C Programming forums, part of the General Programming Boards category; i just started this code. this code will be read records from text file and put to the linked list. ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    26

    i want write contact records with linked list

    i just started this code. this code will be read records from text file and put to the linked list. search/delete/update a contact by first name, last name and city. there are also other functions like insert.

    at the first, i want try read one person's contacts, put to the list and print it.

    my text file like:

    first name
    last name
    date of born
    mail
    phone
    adres
    city

    my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct node{
    	char fname[50];
    	char lname[50];
    	int born;
    	char mail[20];
    	int phone;
    	char address[50];
    	char city[10];
    	int zip;
    	struct node* fnnext;
    	struct node* lnnext;
    	struct node* citynext;
    }NODE;
    
    void print(NODE* head)
    {
    	int y;
    	NODE* tmp;
    	tmp=head;
    	tmp=(NODE*)malloc(sizeof(NODE));
    	tmp->fnnext=NULL;
    	tmp->lnnext=NULL;
    	tmp->citynext=NULL;
    
    	while(tmp!=NULL)
    	{
    	printf("%s", tmp->fname);
    	printf("%s", tmp->lname);
    	printf("%d", tmp->born);
    	printf("%s", tmp->mail);
    	printf("%d", tmp->phone);
    	printf("%s", tmp->address);
    	printf("%s", tmp->city);
    	printf("%d", tmp->zip);
    
    	tmp=tmp->fnnext;
    	tmp=tmp->lnnext;
    	tmp=tmp->citynext;
    	}
    return 0;
    }
    int main(void) {
    
    	NODE* head;
    	head=(NODE*)malloc(sizeof(NODE));
    	head->fnnext=NULL;
    	head->lnnext=NULL;
    	head->citynext=NULL;
    	int m=0;
    	int a;
    	char c;
    
    	FILE *fp;
    	fp=fopen("text.txt", "r+");
    
    	while(!feof(fp))
    	{
    
    		NODE* tmp;
    		tmp=head;
    		tmp=(NODE*)malloc(sizeof(NODE));
    		tmp->fnnext=NULL;
    		tmp->lnnext=NULL;
    		tmp->citynext=NULL;
    
    		fgets (tmp->fname , 50 , fp);
    		fgets (tmp->lname , 50 , fp);
    		fgets (tmp->born , 10 , fp);
    		fgets (tmp->mail , 20 , fp);
    		fgets (tmp->phone , 10 , fp);
    		fgets (tmp->address , 50 , fp);
    		fgets (tmp->city , 10 , fp);
    		fgets (tmp->zip , 10 , fp);
             }
    
    return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Is there a specific question about anything that you have?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Don't control loops with feof... read the FAQ.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    26
    Quote Originally Posted by claudiu View Post
    Don't control loops with feof... read the FAQ.
    i changed loop to " while((c=fgetc(fp))!= EOF)" and write "printf(head)" after the loop. but it still don't work

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    		fgets (tmp->fname , 50 , fp);
    		fgets (tmp->lname , 50 , fp);
    		fgets (tmp->born , 10 , fp);
    		fgets (tmp->mail , 20 , fp);
    		fgets (tmp->phone , 10 , fp);
    		fgets (tmp->address , 50 , fp);
    		fgets (tmp->city , 10 , fp);
    		fgets (tmp->zip , 10 , fp);
             }


    Why do all that when you can just read and write the record itself...
    Code:
    int SaveData(struct node)
      { return fwrite(node, sizeof(node), 1, file); }
    
    int LoadData(struct *node)
      { return fread(node, sizeof(node), 1, file); }
    Yep, easy as pie.
    Last edited by CommonTater; 05-03-2011 at 02:04 PM.
    MK27 likes this.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,247
    Why do all that when you can just read and write the record itself...
    Sure... so long as he still manually corrects for the node pointers, doesn't expect that to work across platforms, doesn't add members, doesn't change the order of the members, doesn't add space to the character arrays, and doesn't change the character array to allocate for storage at runtime.

    Soma
    quzah likes this.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by phantomotap View Post
    Sure... so long as he still manually corrects for the node pointers, doesn't expect that to work across platforms, doesn't add members, doesn't change the order of the members, doesn't add space to the character arrays, and doesn't change the character array to allocate for storage at runtime.

    Soma
    And doesn't waste his time reading you... I've been using structs in record based filing systems for a very long time across 2 different languages and the only time I ran into any problems with it at all is when people start listening to all the reasons it can't work from people who've never done it...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by phantomotap View Post
    Sure... so long as he still manually corrects for the node pointers, doesn't expect that to work across platforms, doesn't add members, doesn't change the order of the members, doesn't add space to the character arrays, and doesn't change the character array to allocate for storage at runtime.

    Soma
    To be fair, any time you change your fixed size records to some other size you have to convert your data to match. But yes, he'll still need to manually match pointers. But that should be easy enough.
    Code:
    int loaddb( struct node **head )
    {
        FILE *fp = NULL;
        fp = fopen( ... );
        if( fp )
        {
            struct node *n = NULL;
            do
            {
                n = malloc( sizeof *n );
                if( n && LoadData( n, fp ) && link( head, n ) )
                    ... yay ...
                else
                    ... boo ...
            } while( !feof( fp ) ); /* Oh I went there! */
        ...
        }
    }
    Although I don't know why LoadData and SaveData are using a global file pointer.


    Quzah.
    Last edited by quzah; 05-03-2011 at 02:59 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In the case of loading a record it would be simple enough to modify the load function like this...
    Code:
    node * LoadData(void)
      { node *ret = malloc(sizeof(node));
         if (!ret)
           return NULL;
    
         if (fread(ret,sizeof(node),1,File) == sizeof(node))
           return ret; 
         else 
           { free(ret);
              return NULL; } }
    He should be able to assign that directly to a head or next pointer, then all he has to do is set up the links in the loaded structure.
    Last edited by CommonTater; 05-03-2011 at 03:03 PM.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,247
    To be fair, any time you change your fixed size records to some other size you have to convert your data to match.
    Which would require having the code to read both binary formats in the application or providing a tool to change the binary format.

    Or, you could just do it right and actually specify the binary characteristics and write code to handle those characteristics in the first place.

    Soma

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by phantomotap View Post
    Which would require having the code to read both binary formats in the application or providing a tool to change the binary format.

    Or, you could just do it right and actually specify the binary characteristics and write code to handle those characteristics in the first place.

    Soma
    Yes, it's usually done with a conversion tool... pass it over the file once, install the updated software and, golly gee, just like nothing happened...

    But, more than that, I'm wondering 2 things...
    Why are your messages always about shooting things down?
    and
    When is the last time you actually posted any code to help anyone?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by phantomotap View Post
    Which would require having the code to read both binary formats in the application or providing a tool to change the binary format.

    Or, you could just do it right and actually specify the binary characteristics and write code to handle those characteristics in the first place.

    Soma
    That's just silly. There are countless - and I do mean countless - applications out there that use fixed size records. Literally tens of thousands, if not millions. Every PC game ever made (yes, I'm sure you can find an extremely rare exception) uses fixed size records. None of them need to provide conversion tools. (Let's completely ignore every console game ever made for a moment!) They just patch it (assuming they patch at all) and are done. Most save games don't have a bunch of fields with arbitrary lengths, because typically none of that stuff needs to be flexible.

    No one cares what you want to call your guy in Diablo. They just give you X characters and if you don't like it, that's just tough. They don't need to make it so you can name your guy something 10,000 characters long, just so their save data is flexible, because that would just be stupid.

    I liked the fact where you brought attention to needing to make sure your pointers line up - because it is something you need to do if you've never worked with binary/fixed records -> a linked list before. But the rest of this is just laughable on your part.


    Quzah.
    Last edited by quzah; 05-03-2011 at 03:14 PM.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Inventory packages are excellent examples of using fixed records, accessed randomly according to ordered part numbers. Not only is it blazing fast, it can easily be indexed for other search criteria... I've written several over the years... one to inventory/index a 100,000 item electronics parts inventory... Using random access getting the last record is every bit as fast as getting the first (binary searhes are fast!) and as a nice bonus you can update records in place, no need to rewrite the entire file because some idiot retyped the description in one of the records.

    Maybe that's why I don't get this C fascination with linked lists loaded into memory... It's slow, it's clunky and it's a pain in the butt to work with.
    Last edited by CommonTater; 05-03-2011 at 03:50 PM.

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,247
    When is the last time you actually posted any code to help anyone?
    I pretty much never post code; I think the questioner should write code. Thanks for asking.

    That's just silly. There are [...] and are done..
    Correct. Well, done. Congratulations. Do you want to know how they manage that?

    By specifying the formats the game loader expects.

    They DO NOT simply throw whatever binary chunk the compiler throws at them into a file.

    Most save games don't have a bunch of fields with arbitrary lengths, because typically none of that stuff needs to be flexible.
    And you think the only problem with a simply dump of the binary chunk the compiler may hand you in the face of the problems I referenced is supporting arbitrary lengths?

    Wow. I didn't mention arbitrary lengths anywhere. That is quite the jumped you've managed.

    Soma

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by phantomotap View Post
    I pretty much never post code;
    Because you won't... or because you can't?

    Seriously... in 8 months on this board I don't believe I have ever seen you actually answer anyone's question or offer a coding suggestion of any kind...

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  2. read/write linked list
    By lambs4 in forum C Programming
    Replies: 4
    Last Post: 03-29-2003, 05:38 PM
  3. Replies: 4
    Last Post: 03-22-2002, 04:30 PM
  4. read/write random records from file
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 09-27-2001, 05:19 AM
  5. read/write random records from file
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-27-2001, 03:01 AM

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