1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

EMERGENCY!!!How to make a database using string function in C

Discussion in 'C' started by askmewhy25, Jan 29, 2010.

  1. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    I have a homework which should have the following:

    • program stores strings of arrays. strings can be names, addresses, etc.
    • program shows selection screen that allows the user to enter strings(max. of 16 strings have a maximum of 128 characters), remove a string from the database, view string s in the database, search a string and quit the program.
    • last the program allow the user to save strings and load strings to and from a text file. this requirements requires the use of I/O
     
  2. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jan 29, 2010
  3. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    What, specifically, is the problem?
     
  4. askmewhy25

    askmewhy25 New Member

    Joined:
    Jan 24, 2010
    Messages:
    49
    Likes Received:
    0
    Trophy Points:
    0
    my program does not compile..there are errors in line 102 and 110..

    ptr->name=name;
    ptr->company=company;

    these are the code which i am having trouble..
     
  5. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Those are pointers to arrays of characters. the equal ('=') operator only works on POD (plain old data) types or classes that implement the equal operator. For C-strings, you need the standard C library string copy functions like strncpy():

    Code:
      strncpy(ptr->name,name,29);
    
      //...
    
      strncpy(ptr->company,company,49);
    
    
     
    shabbir likes this.

Share This Page