Thread: create a book library without linked lists

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    create a book library without linked lists

    Hello! I need to create a book library using dynamic memory allocation and structs.
    I started the program but having a hard time finishing it.. I don't really know how should i organize the program and what's missing in order to finish it.
    i need to take the commends from a simulation file. and the compiling needs to be in GCC.

    the simulation file looks like this:
    #lines which starts with # you shall be able to ignore in your code
    #1 is the add book operation id, BOOK ID, BOOK NAME,author,pages number, publish year, category
    1,A250,Adventures of Huckleberry Finn,Mark Twain,120,1884,adventure
    #2 operation id, book id
    2,A250
    #3 operation id, book id
    3,A250
    #4 operation id
    4
    #5 operation id, file name
    5, C:\bookStorage.txt
    #6 operation id, file name
    6, C:\bookStorage.txt

    explanation about each command:
    1. add a book
    2. delete a book
    3.search a book by it's serial number
    4. print out all the books
    5.save the book library in a file
    6. get the book library data from a file


    this is what i did so far:

    Code:
    typedef struct{
    	int serial;
    	int pg_num;
    	int y_published;
    	char* name;
    	char* author;
    	char* category;
    }Book;
    
    typedef struct(
    	Book* b;
    	int book_num;
    	int length;
    }Book_Shelf;
    
    typedef struct{
    	int command_number;
    	Book b;
    	char* file_name;
    }Command;
    
    void set_command(command* c, char* str);
    
    int main(int argc, char* argv[]){
    
    	FILE* pf;
    	char buffer[512];
    	Command c;
    	
    	pf = fopen(argv[1], "r");
    	
    	fgets(buffer, 512, pf);
    	while (buffer[0] == '#'){
    		fgets(buffer, 512, pf);
    	}
    	
    	set_command(c, buffer);
    	
    	command_execute(c);
    	
    	return 0;
    	
    }
    
    void add_book(Book_Shelf* bs, Book* b){
    	
    	if (bs->book_num == bs->length){
    		bs->length *= 1.5;
    		bs->b = (Book*)realloc(bs->b, length*sizeof(Book));
    		if(bs->b==NULL)
    			exit2;
    
    	}
    	
    	bs->b[book_num].serial = b->serial;
    	/* ... */
    	
    	
    }
    
    char* get_next_word(char* str, int* index){
    
    	int i, length = 0;
    	char* temp_str;
    	
    	for (i = 0; (str[i] != ',') && (str[i] != '\0'); i++, length++);
    	length++;
    
    	temp_str = (char*)malloc(length);
    	for (i = 0; i < length; i++){
    		temp_str[i] = str[i];
    	}
    	temp_str[length] = '\0';
    	
    	*index = i;
    	
    	return temp_str;
    
    }
    
    void set_command(command* c, char* str){
    	
    	int i = 0;
    	
    	c->command_number = atoi(*(get_next_word(str+i, &i)));
    	
    	switch(c->command_number){
    		case 1:{
    			c->b.serial = get_next_word(str+i, &i);
    			c->b.name = get_next_word(str+i, &i);
    			c->b.author = get_next_word(str+i, &i);
    
    		}
    	}
    }
    Last edited by limilou; 03-22-2011 at 08:25 AM. Reason: no tags

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Please re-edit your message so that your code is in code tags...
    << !! Posting Code? Read this First !! >>

    Next please indicate specific areas you're having problems with or ask specific questions.

    Most of us are happy to lend a hand with errant code but as a rule we don't write code for you...

    Right off the bat... this isn't going to work...
    Code:
    typedef struct{
        int serial;
        int pg_num;
        int y_published;
        char* name;
        char* author;
        char* category;
    }Book;
    You have pointers to char in your struct... think about what happens when you write that struct to disk and then reload it...

    When a struct is likely to end up in some storage medium you need to keep all of it's data inside itself... Like this...

    Code:
    typedef struct{
        int serial;
        int pg_num;
        int y_published;
        char name[64];          //-- make these big enough to store the longest string you're
        char author[64];        //    likely to encounter.
        char category[16];
    }Book;
    Last edited by CommonTater; 03-22-2011 at 08:00 AM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    but i need the names (strings) with dynamic allocation.. so i can get any name without wasting memory..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe so, but working with arrays means you can get up and running a lot sooner.

    When it all works, you can then (quite easily, and once only) translate each array into its char* equivalent with malloc.

    Unless you're rock-solid on using malloc, this will give you a quicker working program you can develop further. Until then, use of malloc bugs will just frustrate your progress.

    Also, you're only reading the first command with your main.
    Code:
    	while (fgets(buffer, 512, pf) != NULL ){
    		if ( buffer[0] == '#' ) continue;
    		set_command(c, buffer);
    		command_execute(c);
    	}
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by limilou View Post
    but i need the names (strings) with dynamic allocation.. so i can get any name without wasting memory..
    Believe me, you don't want to do that... The first time you try to write one of your book structs to disk then read it back, you will find out exactly why.

    Moreover... consider that with the string inside the struct you are using exactly the number of characters indicated... char name[40] uses exactly 40 characters. But, with dynamic strings you have the overhead of the pointer... so with a pointer to 40 characters you are actually using 44 characters... only 4 of which will get written to the disk.
    Last edited by CommonTater; 03-22-2011 at 08:52 AM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    i'm really new to c so i'm going to take your advice

    i have a question.. when i want to exit i type...? what?

    and how can amplify a binary search function?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your menu() function, (which serves as a hub for your program), you can use a large
    Code:
    do {
       //all your menu display code in here 
      //including [Q]uit
    
      //get user choice
    
    
      //switch or if statements to call right functions depnding
      //on what the user chose
    
    
    }while(choice != 'q' && choice != 'Q');
    There are a lot of binary search codes on this forum, why don't you search for one, using the search tool, and study it?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The C standard defines the bsearch() function, which works very quickly on any sorted data. You can sort it with the qsort() function, also defined by the standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists with multiple programs
    By crimsonpetal13 in forum C Programming
    Replies: 21
    Last Post: 02-22-2010, 10:56 PM
  2. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  3. Linked Lists
    By xddxogm3 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2003, 03:14 PM
  4. linked lists problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-17-2002, 10:55 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM