Thread: help to modify record in txt (binary)

  1. #1
    Unregistered
    Guest

    help to modify record in txt (binary)

    1) I'm not sure how to modify record stored in txt.
    pls some help me in correcting the code

    2) and also for the VIEW ALL RECORD.. it displays the last record twice.. y?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    struct date_en{
    	int dd,mm,yy;
    };
    
    struct data{
    	int id;
    	char name[20];
    	char addy[30];
    	int age;
    	struct date_en date;
    };
    
    add_new_rec(FILE *ptr)
    {
    	ptr=fopen("details.dat","a+");
    	struct data info;
    	printf("Enter ID: ");
    	info.id=0;
    	while(info.id < 1000 || info.id > 9999)
    	{
    
    		scanf("%d",&info.id);
    	}
    	printf("\nEnter Name: ");
    	scanf(" %[^\n]",info.name);
    	fflush(stdin);
    	printf("\nEnter Addy: ");
    	scanf(" %[^\n]",info.addy);
    	fflush(stdin);
    	printf("\nEnter age: ");
    	scanf("%d",&info.age);
    	printf("Enter date: ");
    	scanf("%d",&info.date.dd);
    	printf("/");
    	scanf("%d",&info.date.mm);
    	printf("/");
    	scanf("%d",&info.date.yy);
    	fwrite(&info,sizeof(struct data),1,ptr);
    	fclose(ptr);
    	return 0;
    }
    
    void view_all_rec(FILE *fptr)
    {
    	struct data info;
    	fptr=fopen("details.dat","r");
    	rewind(fptr);
    	while(!feof(fptr))
    	{
    		fread(&info,sizeof(struct data),1, fptr);
    		printf("%d\n",info.id);
    		printf("%s\n",info.name);
    		printf("%s\n",info.addy);
    		printf("%d\n",info.age);
    		printf("%d/%d/%d",info.date.dd,info.date.mm,info.date.yy);
    		printf("\npress any key for next record!");
    		getch();
    	}
    	fclose(fptr);
    }
    
    void view_rec(FILE *ptr)
    {
    	struct data info;
    	ptr=fopen("details.dat","r");
    	int searchkey,found=0;
    	printf("Input ID no to search");
    	scanf("%d",&searchkey);
    	rewind(ptr);
    	while(!feof(ptr) &&  found==0 )
    	{
    		fread(&info,sizeof(struct data),1, ptr);
    		if (info.id == searchkey)
    		{
    			found=1;
    			printf("%d\n",info.id);
    			printf("%s\n",info.name);
    			printf("%s\n",info.addy);
    			printf("%d\n",info.age);
    			printf("%d/%d/%d",info.date.dd,info.date.mm,info.date.yy);
    			getch();
    		}
    		else
    			found=0;
    	}
    
    	fclose(ptr);
    }
    
    void modify_rec(FILE *ptr)
    {
    	struct data info;
    	ptr=fopen("details.dat","r+");
    	char save;
    	int mod ;
    	printf("Input ID no to search");
    	scanf("%d",&mod);
    	rewind(ptr);
    	while(!feof(ptr))
    	{
    		fread(&info,sizeof(struct data),1, ptr);
    		if (info.id == mod)
    		{
    			printf("\nEnter Name: ");
    			scanf(" %[^\n]",info.name);
    			fflush(stdin);
    			printf("\nEnter Addy: ");
    			scanf(" %[^\n]",info.addy);
    			fflush(stdin);
    			printf("\nEnter age: ");
    			scanf("%d",&info.age);
    			printf("Enter date (dd-mm-yy): ");
    			scanf("%d",&info.date.dd);
    			printf("/");
    			scanf("%d",&info.date.mm);
    			printf("/");
    			scanf("%d",&info.date.yy);
    			printf("\n\n MODIFY SAVE? Y/N  : ");
    			scanf("%s",&save);
    			if ((save =='Y') ||(save =='y'))
    			{
    			// fseek?
    				fwrite(&info,sizeof (struct data),1,ptr);
    				printf("DATA HAVE BEEN UPDATED !!");
    				getch();
    			}
    		}
    		else
    			printf("Record not found");
    
    	}
    	fclose(ptr);
    }
    
    int print_menu()
    {
    	int choice;
    	printf("##########################\n");
    	printf("#   1.Add New Record     #\n");
    	printf("#   2.View Record        #\n");
    	printf("#   3.View All Record    #\n");
    	printf("#   4.Modify Record      #\n");
    	printf("    5.Quit                \n");
    	printf("##########################\n");
    	printf("Please choose what option you want:");
    	scanf("%d",&choice);
    	return choice;
    }
    
    main()
    {
    	FILE *ptr;
    	char dum;
    	int choose;
    	if((ptr = fopen ("details.dat","r"))==NULL){
    		ptr = fopen ("details.dat","w");
    	}
    	do {
    	choose=print_menu();
    	switch(choose)
    		{
    			case 1: add_new_rec(ptr);
    				break;
    			case 2: view_rec(ptr);
    				break;
    			case 3:
    					view_all_rec(ptr);
    					break;
    			case 4:
    					modify_rec(ptr);
    					break;
    			default:
    				printf("Error - Wrong Option!");
    				scanf("%c",&dum);
    				getch();
    				clrscr();
    		}
    	} while (choose !=5);
    	return 0;
    }

  2. #2
    Unregistered
    Guest
    any programmer?
    pls help..

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is the fact that you're using text. If your arguments are potentially variable length, ie: strings of varying sizes, then you cannot easily modify them. Use binary with fixed length records for pure ease of use. Nothing beats it for ease of implementation.

    Otherwise, you have to read all records into memory, and output them all over again. Or rather, read all records starting with the one to modify, then from that point on, overwrite the remainder of the file with the new data + old data that was beyond the record you wish to modify.

    Just a note: 'fflush(stdin)' is not defined and will not work with all compilers.

    One final note: Since you have fixed length records, there is absolutely no point in having text mode files. Use binary.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    void view_all_rec(FILE *fptr)
    {
    	struct data info;
    	//Add this:
    	size_t items;
    	fptr=fopen("details.dat","r");
    	rewind(fptr);
    	while(!feof(fptr))
    	{
    		fread(&info,sizeof(struct data),1, fptr);
    		//Change to:
    		items = fread(&info,sizeof(struct data),1, fptr);
    		if (items > 0)
    		{
    		printf("%d\n",info.id);
    		printf("%s\n",info.name);
    		printf("%s\n",info.addy);
    		printf("%d\n",info.age);
    		printf("%d/%d/%d",info.date.dd,info.date.mm,info.date.yy);
    		printf("\npress any key for next record!");
    		getch();
    		}
    	}
    	fclose(fptr);
    }
    .
    .
    void modify_rec(FILE *ptr)
    {
    	struct data info;
    	ptr=fopen("details.dat","r+");
    	char save;
    	//Change to:
    	char save[10];
    
    .
    .
    			printf("\n\n MODIFY SAVE? Y/N  : ");
    			scanf("%s",&save);
    			//Change to:
    			scanf("%s",save);
    
    			if ((save =='Y') ||(save =='y'))
    			//Change to:
    			if ((save[0] =='Y') ||(save[0] =='y'))
    			{
    				// fseek?
    				// Yes.  Add this:
    				fseek(ptr,(long) (int) -sizeof(struct data),SEEK_CUR);
    				fwrite(&info,sizeof (struct data),1,ptr);
    				printf("DATA HAVE BEEN UPDATED !!");
    				getch();
    				//Add this:
    				break;
    			}

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem you're having is this:

    string[40]

    What happens if string only contains "hello", and you write that with fwrite() to a text file? Does it write all 40 characters? Hell no it doesn't.

    That is the reason you HAVE TO use binary. You can't expect string to be 40 bytes in size when you read it. It won't be. There is nothing you can do that will MAKE IT be 40 bytes in text mode if you haven't filled it entirely. It simply will NOT be 40 bytes. This is your whole problem. You CANNOT do this in text mode. You MUST use binary mode.

    The reason you "must" is because of the way that you're reading and writing.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. behind and confused
    By steviecrawf in forum C Programming
    Replies: 1
    Last Post: 11-09-2001, 12:51 PM