Finish my DATABASE using C

This is a discussion on Finish my DATABASE using C within the C Programming forums, part of the General Programming Boards category; My C program for making a database which stores data and from which data can be retrieved is stated later.But ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Thumbs up Finish my DATABASE using C

    My C program for making a database which stores data and from which data can be retrieved is stated later.But I am not able to write the function for editing the stored items correctly.Please suggest a method.Also suggest :
    1.mode in which file is to be opened in case 2 of the switch
    2.Due to usage of scanf functions in case 1,strings with whitespaces cannot be entered correctly.Also if scanf("[^\n]",..)or gets(...) are used then problem is: stray '\n' is taken as input for the strings instead of taking any string from the user---please suggest!

    Code:
    #include<stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <stdlib.h>
    
    typedef struct {
    		char name[30];
    		char company[50];
    		char address[200];
    		char pin[7];
    		char web[30];
    		}entry;
    char name[30];char company[50];											
    FILE *fpt;int menu(void);
    entry *ptr,commodity;
    int n=sizeof(entry);char ch,y;
    void edit(entry *);
    void main()
    {
    	int choice;
    	do
    	{
    	choice=menu();
    	switch(choice)
    	{
    		case 1:ptr=(entry *)malloc(sizeof(entry));
    			fpt=fopen("data5.bin","a");
    			printf("\nEnter the name of the commodity:");
    			scanf("%s",ptr->name);
    			printf("\nEnter the name of the producing company:");
    			scanf("%s",ptr->company);
    			printf("\nEnter the address of the producing company:");
    			scanf("%s",ptr->address);
    			printf("\nEnter the pincode:");
    			scanf("%s",ptr->pin);
    			printf("\nEnter the website of the producing company:");
    			scanf("%s",ptr->web);
    			fwrite(ptr,n,1,fpt);printf("ENTERED\n");
    			fclose(fpt);
    			continue;
    		case 2:ptr=(entry *)malloc(sizeof(entry)); 
    				fpt=fopen("data5.bin","r+");
    		   do 
    		   {
      			if (fread(&ptr->name, sizeof(ptr->name), 1, fpt) == 1 &&
          	fread(&ptr->company, sizeof(ptr->company), 1, fpt) == 1 &&
          	fread(&ptr->address, sizeof(ptr->address), 1, fpt) == 1 &&
          	fread(&ptr->pin, sizeof(ptr->pin), 1, fpt) == 1 && 
          	fread(&ptr->web, sizeof(ptr->web), 1, fpt) == 1)
          			{
        					printf("\n\nThe name of the commodity:");
    						printf("%s",ptr->name);
    						printf("\nThe name of the producing company:");
    						printf("%s",ptr->company);
    						printf("\nThe address of the producing company:");
    						printf("%s",ptr->address);
    						printf("\nPincode:");
    						printf("%s",ptr->pin);
    						printf("\nWebsite of the producing company:");
    						printf("%s",ptr->web);
    						printf("\nWant to EDIT this entry?(Enter y/n):");
    						ch=getchar();
    						if(ch==y) 
    							edit(ptr);
    			     }
       else if (feof(fpt)) 
              {
        printf("\n\nfinished reading file!\n");
        break;
        }
       else {
        printf("an error occured while reading the file!\n");
        break;
      	 }
    } while (1);
    		       fclose(fpt);continue;
    	   case 3:printf("\nThank You for using!");exit(1);      								
    	}
    	}while(choice==1||choice==2);
    	getch();
    }
    int menu(void)
    {
    	int choice;
    	do{
    		printf("Enter your choice:\n");
    		printf("1.ADD a entry\n");
    		printf("2.DISPLAY cum EDIT\n");
    		printf("3.EXIT\n");
    		scanf("%d",&choice);
    	}while(choice!=1 && choice!=2 && choice!=3);
    	return(choice);
    }
    void edit(entry *ptr)
    {
    	printf("\nWant to EDIT name of commodity?(y/n):");
    	ch=getchar();
    	if(ch==y)
    	{		
    			printf("\nEnter new commodity name:");
    			scanf("%s",name);
    			ptr->name=name;
    	}
    	printf("\nWant to EDIT name of company?(y/n):");
    	ch=getchar();
    	if(ch==y)
    	{		
    			printf("\nEnter new company name:");
    			scanf("%s",company);
    			ptr->company=company;
    	}                      //....etc..etc.
    	fwrite(ptr,n,1,fpt);
    	return;
    }
    Last edited by Salem; 07-04-2006 at 12:08 PM. Reason: PHP != CODE tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    > FILE *fpt;int menu(void);
    Don't mix declarations like this on one line

    > void edit(entry *);
    It's useful to also specify a variable name here, just to help document what is going on.
    Eg, which do you prefer
    strcpy ( char*, char *); // OK, but which way ?
    strcpy ( char *dst, char *src ); // much more obvious what gets copied

    > #include <conio.h>
    Is a non-standard header you can well do without.

    > void main()
    Main returns an int
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    > ptr=(entry *)malloc(sizeof(entry));
    Ptr is a poor name for a variable, and a global one is even worse.
    Also, there is no need to cast malloc in C
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    > fpt=fopen("data5.bin","a");
    You should use the "b" mode as well, if you're using binary files (as is typical with fread and fwrite)

    > if(ch==y)
    Don't you mean
    if(ch== 'y' )

    > 2.Due to usage of scanf functions in case 1,strings with whitespaces cannot be entered correctly.Also if scanf("[^\n]",..)or gets(...) are used then problem is:
    You should use fgets() for ALL input, which reads into a buffer
    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    Then you can validate, convert, copy the data to it's correct place.

    Fixing the indentation would also help.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    Unhappy

    Quote Originally Posted by Salem
    >
    Due to usage of scanf functions in case 1,strings with whitespaces cannot be entered correctly.Also if scanf("[^\n]",..)or gets(...) are used then problem is:
    You should use fgets() for ALL input, which reads into a buffer
    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    .
    I tried salem's method but the same problem persists.i.e.,during run of the prog, at first if I enter choice#1(for entering data) and then press 'enter', then this 'enter' i.e.,'\n' is taken as input for the name of the commodity and then the prog asks for the company name...........suggest a way out.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    '\n' is taken as input for the name of the commodity and then the prog asks for the company name...........suggest a way out.

    Code:
    do
        {
            choice=menu();
            // Flush buffer 
            while ((ch = getchar()) != '\n' && ch != EOF);        
            switch(choice)
            {
                case 1:
                    ptr=(entry *)malloc(sizeof(entry));
                    fpt=fopen("data5.bin","a");

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Yeah, you get that if you mix fgets() and scanf().
    Use fgets() for EVERYTHING, then use sscanf(), or whatever else seems appropriate.
    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. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 12:34 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  3. Replies: 10
    Last Post: 05-19-2006, 12:23 AM
  4. Developing database management software
    By jdm in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 05:06 PM
  5. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 10:17 PM

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