Thread: Inventory System

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    31

    Inventory System

    Just starting out in the C language. Learning it as my first language, and I'm really enjoying it. My first major program is now 95% complete, a little cheat program for people in school for maths and science work, it taught me a lot, but it's time to move on.

    I want to have a go at an inventory system, now I'm thinking of using a mySQL database to store various product information, then use a C command line program as an interface. How easy is this for a beginner. I'd also to generate a word document with various product information or receipts, again how easy is this.

    Any information greatly received.
    Last edited by headshot119; 09-23-2009 at 12:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Probably not that easy. Do you already know SQL? How about the Word document format?

    If you are already familiar with SQL and Word, then that will help quite a bit. If not, then you may find this project a little overwhelming.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Not particularly familiar with SQL, could Open Office Access be used for the database part, I'm familiar with that.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are C libraries for dealing with SQL db's. I haven't used one (in C) but probably they are pretty simple, so it would be a good first API to learn.

    Google "C API SQL" and you will find stuff. Eg, this is part of mySQL:

    MySQL :: MySQL 5.0 Reference Manual :: 20.9.2 C API Function Overview

    I would recommend going with SQL in some form, it is not hard to learn and much more portable than some Open Office thing; understanding how SQL works is a pretty useful skill and the knowledge is applicable to most other languages as well (eg. php). Here's the tutorial I used:

    http://www.firstsql.com/tutor1.htm

    Which is just about Structured Query Language itself.

    If all this is looking like a bit much and you want more practice doing basic things in C, creating your own database would be a great idea. You do not have to use SQL or anything, the concept of "a database" is straightforward enough, you just need an idea for the implimentation.
    Last edited by MK27; 09-23-2009 at 12:36 PM.
    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

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by headshot119 View Post
    Not particularly familiar with SQL, could Open Office Access be used for the database part, I'm familiar with that.
    I've never used Open Office Access, so I'm not sure.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Quote Originally Posted by MK27 View Post
    If all this is looking like a bit much and you want more practice doing basic things in C, creating your own database would be a great idea. You do not have to use SQL or anything, the concept of "a database" is straightforward enough, you just need an idea for the implimentation.
    I assume I could use some sort of multidimensional array to create the database, but how can I save the data once I exit the program? This may be the easier way to do the program initially.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by headshot119 View Post
    I assume I could use some sort of multidimensional array to create the database, but how can I save the data once I exit the program? This may be the easier way to do the program initially.
    You can always save it to a text file. Check into the following I/O Functions:

    fopen()
    fclose()
    fread()
    fwrite()
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by headshot119 View Post
    I assume I could use some sort of multidimensional array to create the database, but how can I save the data once I exit the program? This may be the easier way to do the program initially.
    You save it to a file. I would use an array of structs for a database, since a single struct equates very well with the concept of a single record, for example:
    Code:
    struct record {
           int ID;
           char partname[256];
           char partnum[64];
           char *data;
    }
    IMO, the best way to save the data is save it in text form. The other alternative is to serialize the struct array and write it to a binary file. This will be faster and POSSIBLY slightly easier BUT there are some potentially difficult complications if you are not comfortable with structs, pointers and serialization (don't eat too much at once!).

    With a text file, it is very easy to verify that your program works, because you can visually inspect the database. You can simply use one line per record -- even if the line is several hundred (or even a thousand) characters long, that's fine. So you need two functions, one to take a struct/record and turn into a line written into the text file, and another one to read a line from the file and arrange the data properly into a struct. That will probably mean using fgets(), sscanf(), and/or strtok().

    The first thing you should do is get a handle on how to create and populate a struct, how to create an array of structs, and how to pass a pointer to a struct around and a pointer to an array of structs around. After that, you incorporate reading/writing to a file, which is very easy.

    You could use a linked list here but that is not necessary, an array will be fine.

    My other note would be that using an int ID with database records will come in handy later -- this has nothing to do with the record data, it is just a unique number for each record, eg, 1, 2, 3, 4, etc. making sure no two records have the same ID.
    Last edited by MK27; 09-23-2009 at 12:54 PM.
    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

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Check out OpenDBX.

    If you're not deadset on an SQL database, you might consider SQLite.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    I've read up a bit on structs MK


    Code:
       struct
       {
           char cname[8];
           char sname[16];   
           char exam[16];
           char grade;
       } record;
    Would define a struct with 4 variables? If I understand that correctly. I could then set

    Code:
       main()
       {
          struct record;
         
          record.a = (Read from text file);
          record.b = (Read from text file);
          record.c = (Read from text file);
       }
    Am I on the right lines?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by headshot119 View Post
    Am I on the right lines?
    Yes. Except the "record" part should be where I put it, not where you put it, the difference being you are creating something that is a pointer, which you don't want to do.

    I'm actually writing a little demo for you but have gotten a little carried away, gimme a few minutes Do not worry about reading from the file yet, keep thinking about structs and what you want to do with them, just hardcode the content for now...
    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

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Thanks for making a demo, I should have time tomorrow to work on a hard coded version of the database.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I found MySQL pretty straight forward. I wrote this some time ago. It worked on my Mac.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #include <memory.h>  
    #include </usr/local/mysql/include/mysql.h> 
    
    #define DBNAME "todd" 
    #define TBNAME "products" 
    
    MYSQL * mysql ; 
    
    int do_sql(char * sqltext ) { 
    	if (mysql_query(mysql, sqltext) ) { 
    		fprintf(stderr, "SQL Statement failed: >%s<\nError %d: %s\n", 
    			sqltext, mysql_errno(mysql), mysql_error(mysql) ) ; 
    		return 0 ;  // error 
    	}
    	return 1 ; // all is OK 
    }
    
    
    int main (int argc, const char * argv[]) {
    
    	int i ; 
    	char create_db[] = "CREATE DATABASE " DBNAME " ; " ; 
    	char create_tb[] = "CREATE TABLE " DBNAME "." TBNAME " ( " 
    		"col1 INTEGER NOT NULL" 
    		") ; " ; 
    	char insert[] = "INSERT INTO " DBNAME "." TBNAME " (col1) values ( %d ) ; " ; 
    	char drop_db[] = "DROP DATABASE " DBNAME " ; " ;  
    	char select[] = "SELECT * FROM " DBNAME "." TBNAME " ORDER BY col1 DESC ; " ; 
    	char temp_stmt[81] ;  
    	MYSQL_RES * result_set ; 
    	MYSQL_ROW row ; 
    	int numcols ; // , numrows ; 
    	
    	
    	mysql = calloc(1, sizeof(MYSQL)) ; 
    	mysql_init(mysql) ; 
    
    	if (!mysql_real_connect(mysql,"localhost", "root", "changeme", NULL , 0, NULL ,0)) { 
    		fprintf(stderr, "Unable to connect. Error %d: %s\n", mysql_errno(mysql), mysql_error(mysql)) ; 
    		return -1 ; 
    	}
    	else printf("Connected!\n") ; 
    
    	// Drop the Database if it exists 
    	if (do_sql(drop_db)) {  
    		printf("Database " DBNAME " dropped!\n") ; 
    	} 
    
    	// Create the Database 
    	
    	if (!do_sql(create_db)) {  
    		fprintf(stderr, "exiting.\n") ;  
    		return -1 ; 
    	} 
    	else { 
    		printf("Database " DBNAME " created!\n") ; 
    	} 
    	
    	if (!do_sql(create_tb)) {  
    		fprintf(stderr, "exiting.\n") ;  
    		return -1 ; 
    	} 
    	else { 
    		printf("Table " TBNAME " created!\n") ; 
    	} 
    	// Insert into the table a few rows 
    	for (i=0 ; i < 10 ; i++ ) { 
    		sprintf(temp_stmt, insert, i+1) ; 
    		printf("Insert = >%s<\n", temp_stmt) ; 
    		if (!do_sql(temp_stmt)) { 
    			fprintf(stderr, "exiting insert loop...\n") ;  	
    			break ; 
    		} 
    	}
    	
    	
    	printf("Inserted %d rows.\n", i) ; 
    	
    	// Now, select the Rows.   
    	
    	if (!do_sql(select)) {  
    		fprintf(stderr, "exiting.\n") ;  
    		return -1 ; 
    	} 
    	else { 
    		printf("SELECT was OK!\n") ; 
    	} 
    	
    	
    	// Get the handle for the result table 
    	
    	result_set = mysql_use_result(mysql) ; 
    
    	if (!result_set) { 
    		fprintf(stderr, "use_result failed.  Error: %d, %s\n", 
    			mysql_errno(mysql), mysql_error(mysql) ) ;  
    		return -1 ; 
    	} 
    	else { 
    		printf("Use Result worked!\n") ; 
    	} 
    	
    	numcols = mysql_num_fields(result_set) ; 
    	printf("There are %d columns in the result table.\n", numcols );
    
    	while  ( row = mysql_fetch_row(result_set) ) {
    		for ( i = 0 ; i < numcols ; i++) {
    			printf("Row %d value = %s\n", i+1, row[i] );
    		}
    	}
    	
        return 0;
    }
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by headshot119 View Post
    Thanks for making a demo, I should have time tomorrow to work on a hard coded version of the database.
    Okay! I had forgotten there is a potentially confusing caveat, lemme get to that:
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* malloc, realloc, free */
    #include <string.h>
    
    struct record {
    	int ID;
    	char data[64];
    };
    
    int Total = 0;
    
    struct record **create (char *string, struct record **rp) {
    	static int id = 1;
    	struct record *ptr = malloc(sizeof(struct record));
    	rp = realloc(rp,(Total+1)*sizeof(struct record*));
    	ptr->ID = id++;
    	strncpy(ptr->data,string,63);
    	ptr->data[63] = '\0';
    	rp[Total++] = ptr;
    	return rp;
    }
    
    void show (struct record *ptr) {
    	printf("ID: %d Data: %s\n",ptr->ID,ptr->data);
    }
    
    int main() {
    	int i;
    	struct record **database = NULL;
    	database=create("first record",database);
    	database=create("second record",database);
    	for (i=0; i<Total; i++) show(database[i]);
    	return 0;
    }
    There may be a few new concepts here, you need to deal with all of them.

    First, you do not know how many records there will be, so you need to use a dynamically allocated array:
    Code:
    struct record **database = NULL;
    This has zero storage space right now -- it is assigned in create(). **database is an array of pointers, right now of zero length. This adds a new pointer to the array:
    Code:
    	rp = realloc(rp,(Total+1)*sizeof(struct record*));
    You need to read up on how to use malloc(), realloc(), and free(). Altho I don't use free() here you MUST understand what it is for and how a memory leak happens. Notice I realloc space for a pointer, sizeof(struct record*), but I malloc that pointer space for an entire struct, sizeof(struct record).

    The caveat I mentioned earlier is that you actually have to return the array in order to make it work:
    Code:
    struct record **create (char *string, struct record **rp);
    database=create("first record",database);
    That is because the realloc line reassigns database (aka rp); if you do that in a function, it is only valid within the function itself. So, just to repeat, we must then also return the address so that it is reassigned in main as well.

    The point here is to have some easy to (re)use functions. So create() may seem complex internally, but it is easy to use; adding a new record is simple. Part of the hassle w/ create is because of the internal realloc call. We could simplify it by getting rid of that, just making it create the malloc'd struct pointer and return that, but then everytime we create a record, we would manually have to realloc **database and increase our Total count. That will probably end up as more of a hassle. Perhaps in the end you will want two seperate shorter functions, one to extend **database and one to return a new struct, since there will be a difference between the later and one that is read from the file. But it's fine for now.

    Global variables are often frowned on but IMO the one Total count here is fine. You could get rid of that, then you would have to pass an int pointer to create(). The reason I kept Total and the id count separate is that presumably if you are reading from a file, you will be adding structs without using create, since they already have an ID number. This is probably not how you want to handle that, since the create() method of assigning an ID number is not so smart, but it at least keeps the issue explicit.

    Sorry if this seems confusing, feel free to ask questions. I just have a suspicion you are about to get into the real tish of C syntax with pointers, arrays, "pointers to pointers", and structs and will need as many examples as you can, I spent many hours frustrated with all that way back when
    Last edited by MK27; 09-23-2009 at 04:06 PM.
    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

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Thanks for the example, I've had a quick look and I understand most of it I think, I did compile it but I got some errors.

    I'll take a better look tomorrow after school, and read up on the suggestions you made.

    Thanks for the help, I'm sure I'll ask loads of questions tomorrow.

    Your right, my other programs most exotic function was an if statement. I've read up on arrays and pointers and understand them in the context of arrays, but not in a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inventory system
    By linucksrox in forum Game Programming
    Replies: 0
    Last Post: 04-19-2006, 01:19 PM
  2. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  3. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM
  4. Thoughts on Menu System for Book Inventory
    By curlious in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 03:32 AM
  5. need help with an inventory program
    By lackerson191 in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2001, 09:32 PM