Thread: File handling problem

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

    File handling problem

    Good day!

    I have a problem in making our project since our teacher didn't discuss about file handling. We are to make a customer record using file handling. The program (C) should have the following functions:

    createNew //initializes your file
    addRecord // adds a new record to the file
    updateRecord // selects a record and make changes
    displayFile// points the record in the file in the order they are entered
    searchFile //determines if the record is in the file
    recordCount // determines how many record are in the file
    sortRecord // arranges the record in the file(key specified)

    If possible, you can include menu function for search, update, etc.

    So far, I have these information:

    Code:
    struct 
    {
    	char custID[5];
    	char custName[50];
    	char custAddress[100];
    	char custBday[20];
    	int custAge;
    	char custProdBought[20];
    	char custDateProdBought[20];
    	int custProdPrice;
    }Customer;
    I really hope for an answer, most possible by March 17 (Thursday).

    Thank you very much!:3

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    For file handling, you need to read up on the following functions:
    fopen()
    fclose()
    fread() or fscanf()
    fwrite() or fprintf()
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by echzee View Post
    Good day!

    I have a problem in making our project since our teacher didn't discuss about file handling. We are to make a customer record using file handling. The program (C) should have the following functions:
    Read --->>> Homework Policy <<<--- Read

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Oh. I'm sorry, I'm new to this forum.

    Sorry for violating a policy.

    Maybe I should only ask for some tips

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by echzee View Post
    Oh. I'm sorry, I'm new to this forum.

    Sorry for violating a policy.

    Maybe I should only ask for some tips
    No worries... but one of the things I took from reading your message is that you expected us to write code for you. This is not a free coding service.

    You say your teacher assigned this without first giving you at least one lesson on binary file IO ... Well, that's just stupid. Your class should get together and explain that you need the files lesson first... Perhaps ask for a new assignment more in keeping with your position in the course.

    That said, there should be nothing stopping you from reading ahead in your text books and figuring this out on your own...

    As for the actual problem itself... Well, the best I can suggest is that you come up with more than a rough struct and start pounding some code... You are under 24 hours, to your deadline. If you get stuck --really stuck, not just lazy stuck-- post your code here and we'll see what we can do...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first hint would be read up on fgets() and sscanf()
    Then write some code to read lines of text from the user, then use sscanf() to extract data into an instance of your structure.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your best bet for help is to write what code you can. Then, you can come back, post said code (in code tags of course), and ask us specific questions about what problems you're having.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Ok, so I made the code!YAY.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    
    typedef struct customer c;
    
    struct customer
    {
    	int custID;
    	char custName[50];
    	char custAddress[100];
    	char custBday[20];
    	char custProdBought[20];
    	char custDateProdBought[20];
    	int custProdPrice;
    };
    
    
    
    void createNew(); //initialize your file 
    void addRecord(customer c[]); // add a new record to the file
    void updateRecord(); // select a record and make changes
    void displayFile();// points the record in the file in the order they are entered
    void searchFile(); //determine if the record is in the file
    void recordCount(); // determine how many record are in the file
    void sortRecord(); // arrange the record in the file(key specified)
    void exit();
    
    
    void createNew()
    {
    	FILE *fp;
    
    	fp=fopen("Customer.dat", "r");
    
    	if(fp==NULL)
    		printf("File creation Failed!\n");
    	else
    		printf("File created!\n");
    
    	fclose(fp);
    
    }
    
    
    void addRecord(customer c[])
    { 
    	int i,n,cc=0;
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "a");
    	system("cls");
    
    	if(fp==NULL)
    	{
    		printf("File Creation Failed!");
    	}
    
    	system("cls");
    
    	printf("Enter the number of Customers: ");
    	scanf("%d", &n);
    
    	for(i=0;i<=n;i++)
    	{
    		printf("Customer's ID (numbers only)  : ");
    		scanf("%d", &c.custID);
    
    		printf("Customer's  Name              : ");
    		fflush(stdin);
    		gets(c.custName);
    
    		printf("Customer's Address            : ");
    		fflush(stdin);
    		gets(c.custAddress);
    
    		printf("Customer's Birthday           : ");
    		fflush(stdin);
    		gets(c.custBday);
    				
    		printf("Customer's Last Product Bought: ");
    		fflush(stdin);
    		gets(c.custProdBought);
    				
    		printf("Date Bought                   : ");
    		fflush(stdin);
    		gets(c.custDateProdBought);
    	
    		printf("Product's Price               : ");
    		fflush(stdin);
    		gets(c.custProdPrice);
    	
    		
    		
    		fwrite((char *)&c, sizeof(c), 1, fp);
    	}cc++;
    
    	fclose(fp);
    }
    
    void updateRecord()
    { 
    	int recno;
    	char ch;
    
    
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb+");
    
    	system("cls");
    
    	printf("Enter the Customer's ID to modify : ");
    	scanf("%d", &recno);
    	while((fread((char *)&c, sizeof(c), 1, fp))==1)
    	{
    		
    		if(c.custID==recno)
    		{
    			printf("Customer's ID: %d.\n",c.custID);
    			printf("Customer's Name: %c.\n",c.custName);
    			printf("Customer's Address: %c.\n",c.custAddress);
    			printf("Customer's Birthday: %c.\n", c.custBday);
    			printf("Customer's Last Product Bought: %c.\n",c.custProdBought);
    			printf("Date bought: %c.\n",c.custDateProdBought);
    			printf("Product's Price: %c.\n",c.custProdPrice);
    			printf("\n");
    
    			printf("Do you want to modify this record?(Y/N): ");
    			fflush(stdin);
    			scanf("%c", &ch);
    
    			fseek(fp, ((nofrec-1)*sizeof(c)), 0);
    			if(ch=='Y'|| ch=='y')
    			{
    				printf("Enter customer's last product bought: ");
    				scanf("%c",&c.custProdBought);
    				printf("Enter date bought: ");
    				scanf("%c", &c.custDateProdBought);
    				printf("Enter the product's price: ");
    				scanf("%c", &c.custProdPrice);
    
    				fwrite((char *)&c, sizeof(c), 1, fp);
    				printf("Record was successfully modified.");
    			}
    
    			else
    				printf("No modifications were made.");
    
    			fclose(fp);
    
    		}
    	}
    }
    
    
    void displayFile()
    {
    	int nofrec=0;
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb");
    	if(fp==NULL)
    	{
    		printf("\n\tFile doesn’t exist!!!\nTRY AGAIN.");
    	}
    
    	system("cls");
    
    	while((fread((char *)&c, sizeof(c), 1, fp))==1)
    	{ 
    		nofrec++;
    
    		printf("Customer's ID: %d.\n",c.custID);
    		printf("Customer's Name: %c.\n",c.custName);
    		printf("Customer's Address: %c.\n",c.custAddress);
    		printf("Customer's Birthday: %c.\n", c.custBday);
    		printf("Customer's Last Product Bought: %c.\n",c.custProdBought);
    		printf("Date bought: %c.\n",c.custDateProdBought);
    		printf("Product's Price: %c.\n",c.custProdPrice);
    		printf("\n\n\n");
    
    	}
    	printf("Total number of records present are : %d", nofrec);
    
    	fclose(fp);
    
    }
    
    
    void searchFile()
    {
    	 int s,recno;
    	char sname[20];
    
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb");
    	
    	system("cls");
    	printf("What do you want to search for?\n");
    	printf("[1] Search by Customer's Name.");
    	printf("[2] Search by Customer's ID.");
    	scanf("%d", &s);
    
    	system("cls");
    	switch(s)
    	{
    		case 1:	printf("Enter the Customer's name to search: ");
    			fflush(stdin);
    			gets(sname);
    			while((fread((char *)&c, sizeof(c), 1, fp))==1)
    			{
    				if(strcmp(sname,c.custName)==0)
    				{
    					printf("Customer's ID: %d.\n",c.custID);
    					printf("Customer's Name: %c.\n",c.custName);
    					printf("Customer's Address: %c.\n",c.custAddress);
    					printf("Customer's Birthday: %c.\n", c.custBday);
    					printf("Customer's Last Product Bought: %c.\n",c.custProdPrice);
    					printf("Date bought: %c.\n",c.custDateProdBought);
    					printf("Product's Price: %c.\n",c.custProdPrice);
    					printf("\n");
    				}
    			}
    			break;
    
    
    		case 2:	printf("Enter the Customer's ID to search :" );
    			scanf("%d", &recno);
    			while((fread((char *)&c, sizeof(c), 1, fp))==1)
    			{
    				if(c.custID==recno)
    				{
    					printf("Customer's ID: %d.\n",c.custID);
    					printf("Customer's Name: %c.\n",c.custName);
    					printf("Customer's Address: %c.\n",c.custAddress);
    					printf("Customer's Birthday: %c.\n", c.custBday);
    					printf("Customer's Last Product Bought: %c.\n",c.custProdPrice);
    					printf("Date bought: %c.\n",c.custDateProdBought);
    					printf("Product's Price: %c.\n",c.custProdPrice);
    					printf("\n");
    
    				}
    			}
    			break;
    	}
    }
    
    
    void recordCount(int count)
    {
    	addRecord();
    	count =0;
    	count++;
    }
    
    void sortRecord(customer c[], int size)
    {
    	int x, y;
    
        customer temp;
    	for(x=0 ; x < size - 1 ; x++)
    	{
    		for(y=0 ; y < size - 1 ; y++)
    		{
    
    			if(strcmp(c[y].custName, c[y+1].custName) > 0)
    			{
    				temp = c[y];
    				c[y]= c[y+1];
    				c[y+1]= temp;
    			}
    
    
    		}
    	}
    
    }
    
    
    void exit()
    {
    	system("cls");
    	printf("Goodbye!");
    }
    
    
    void main()
    {
    	customer c[30];
    	 int a,i;
    	char ch;
    		
    	printf("Creating file...\n");
    	createNew();
    
    	printf("Press enter to continue...");
    	getch();
    
    	system("cls");
    
    	do{
    		printf("\nCustomer's Information Data.\n\n");
    		printf("What do you want to do?\n");
    		printf("[1] Add new Customer Information.\n");
    		printf("[2] Update a Customer Information.\n");
    		printf("[3] Display list of record.\n");
    		printf("[4] Search a record.\n");
    		printf("[5] Exit.\n\n");
    
    		printf("\n\n");
    
    		recordCount();
    		printf("\n\n");
    	
    		sortRecord(c,i);
    		do
    		{
    			scanf("%d",&a);
    
    			switch(a)
    			{
    				case 1: addRecord(c);break;
    				case 2: updateRecord();break;
    				case 3: displayFile();break;
    				case 4: searchFile();break;
    				case 5: exit ();
    				default: printf("Invalid Choice!");
    			}
    		}
    		while (a<0 && a>7);
    
    
    		printf("\nDo you want to try again(Y/N)? :");
    		fflush(stdin);
    		scanf("%c", &ch);
    	}	
    	while(ch=='y'|| ch=='Y');	
    }

    But I'm stuck with 9 errors I can't fix. :/

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first thing is to read this -> SourceForge.net: cpwiki
    To fix your unholy trinity of void main, gets() and fflush(stdin)

    > But I'm stuck with 9 errors I can't fix. :/
    Only 9?
    Anyone compiling with anything other that TurboCrap would get a lot more than 9 errors.
    Perhaps you should post the errors you're getting.
    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.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I tried compiling your program in Turbo C, and it filled up to 25 errors. How many beyond that, I couldn't tell you.

    This is a compilable version of your program -- lots of work in here. Note that you had a typedef for c as a struct, and c[] as an array of structs, and I changed that.

    I did not try and run the program, beyond the initial menu.

    Code:
    #include <stdio.h>
    #include <string.h>
    //#include <stdlib.h>  //probably don't need this either
    
    //You don't need these two files IMO.
    //#include <windows.h>
    //#include <conio.h>
    
    struct customer
    {
    	int custID;
    	char custName[50];
    	char custAddress[100];
    	char custBday[20];
    	char custProdBought[20];
    	char custDateProdBought[20];
    	int custProdPrice;
    };
    typedef struct customer c;
    
    
    void createNew(void); //initialize your file 
    void addRecord(c c1[30]); // add a new record to the file
    void updateRecord(void); // select a record and make changes
    void displayFile(void);// points the record in the file in the order they are entered
    void searchFile(void); //determine if the record is in the file
    void recordCount(c c1[30], int *count); // determine how many record are in the file
    void sortRecord(c c1[30], int size); // arrange the record in the file(key specified)
    void exit1();
    
    
    void createNew()
    {
    	FILE *fp;
    
    	fp=fopen("Customer.dat", "r");
    
    	if(fp==NULL)
    		printf("File creation Failed!\n");
    	else
    		printf("File created!\n");
    
    	fclose(fp);
    }
    
    void addRecord(c c1[30])
    { 
    	int i,n,cc=0;
    	FILE *fp;
    	fp=fopen("Customer.dat", "a");
    	system("cls");
    
    	if(fp==NULL)
    	{
    		printf("File Creation Failed!");
    	}
    
    	system("cls");
    
    	printf("Enter the number of Customers: ");
    	scanf("%d", &n);
    
    	for(i=0;i<=n;i++)
    	{
    		printf("Customer's ID (numbers only)  : ");
    		scanf("%d", &c1[i].custID);
    
    		printf("Customer's  Name              : ");
    		gets(c1[i].custName);
    
    		printf("Customer's Address            : ");
    		gets(c1[i].custAddress);
    
    		printf("Customer's Birthday           : ");
    		gets(c1[i].custBday);
    				
    		printf("Customer's Last Product Bought: ");
    		gets(c1[i].custProdBought);
    				
    		printf("Date Bought                   : ");
    		gets(c1[i].custDateProdBought);
    	
    		printf("Product's Price               : ");
                   scanf("%d", &c1[i].custProdPrice);
    		//gets(c[i].custProdPrice); //this is an int, not a string
    		
    		fwrite(&c1[i], sizeof(c), 1, fp);
    	}cc++;
    
    	fclose(fp);
    }
    
    void updateRecord()
    { 
    	int recno, nofrec=0;
    	char ch;
      struct customer cust1;
    
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb+");
    
    	system("cls");
    
    	printf("Enter the Customer's ID to modify : ");
    	scanf("%d", &recno);
           (void) getchar();  //remove the newline from the keyboard stream
    	while((fread(&cust1, sizeof(cust1), 1, fp))==1)
    	{
    		nofrec++;
    		if(cust1.custID==recno)
    		{
    			printf("Customer's ID: %d.\n",cust1.custID);
    			printf("Customer's Name: %c.\n",cust1.custName);
    			printf("Customer's Address: %c.\n",cust1.custAddress);
    			printf("Customer's Birthday: %c.\n", cust1.custBday);
    			printf("Customer's Last Product Bought: %c.\n",cust1.custProdBought);
    			printf("Date bought: %c.\n",cust1.custDateProdBought);
    			printf("Product's Price: %c.\n",cust1.custProdPrice);
    			printf("\n");
    
    			printf("Do you want to modify this record?(Y/N): ");
    			scanf("%c", &ch);
          (void) getchar();
    			fseek(fp, ((nofrec-1)*sizeof(cust1)), 0);
    			if(ch=='Y'|| ch=='y')
    			{
    				printf("Enter customer's last product bought: ");
    				scanf("%c",&cust1.custProdBought);
            (void) getchar();
    				printf("Enter date bought: ");
    				scanf("%c", &cust1.custDateProdBought);
            (void) getchar();
    				printf("Enter the product's price: ");
    				scanf("%c", &cust1.custProdPrice);
            (void) getchar();
    				fwrite(&cust1, sizeof(cust1), 1, fp);
    				printf("Record was successfully modified.");
    			}
    
    			else
    				printf("No modifications were made.");
    
    			fclose(fp);
    
    		}
    	}
    }
    
    
    void displayFile()
    {
    	int nofrec=0;
            c cust1;
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb");
    	if(fp==NULL)
    	{
    		printf("\n\tFile doesn’t exist!!!\nTRY AGAIN.");
    	}
    
    	system("cls");
    
    	while((fread(&cust1, sizeof(cust1), 1, fp))==1)
    	{ 
    		nofrec++;
    
    		printf("Customer's ID: %d.\n",cust1.custID);
    		printf("Customer's Name: %c.\n",cust1.custName);
    		printf("Customer's Address: %c.\n",cust1.custAddress);
    		printf("Customer's Birthday: %c.\n", cust1.custBday);
    		printf("Customer's Last Product Bought: %c.\n",cust1.custProdBought);
    		printf("Date bought: %c.\n",cust1.custDateProdBought);
    		printf("Product's Price: %c.\n",cust1.custProdPrice);
    		printf("\n\n\n");
    
    	}
    	printf("Total number of records present are : %d", nofrec);
    
    	fclose(fp);
    
    }
    
    
    void searchFile()
    {
    	 int s,recno;
    	char sname[20];
            c cust1;
    
    	FILE *fp;
    	fp=fopen("Customer.dat", "rb");
    	
    	system("cls");
    	printf("What do you want to search for?\n");
    	printf("[1] Search by Customer's Name.");
    	printf("[2] Search by Customer's ID.");
    	scanf("%d", &s);
    
    	system("cls");
    	switch(s)
    	{
    		case 1:	printf("Enter the Customer's name to search: ");
    			fflush(stdin);
    			gets(sname);
    			while((fread(&cust1, sizeof(cust1), 1, fp))==1)
    			{
    				if(strcmp(sname,cust1.custName)==0)
    				{
    					printf("Customer's ID: %d.\n",cust1.custID);
    					printf("Customer's Name: %c.\n",cust1.custName);
    					printf("Customer's Address: %c.\n",cust1.custAddress);
    					printf("Customer's Birthday: %c.\n", cust1.custBday);
    					printf("Customer's Last Product Bought: %c.\n",cust1.custProdPrice);
    					printf("Date bought: %c.\n",cust1.custDateProdBought);
    					printf("Product's Price: %c.\n",cust1.custProdPrice);
    					printf("\n");
    				}
    			}
    			break;
    
    
    		case 2:	printf("Enter the Customer's ID to search :" );
    			scanf("%d", &recno);
    			while((fread(&cust1, sizeof(c), 1, fp))==1)
    			{
    				if(cust1.custID==recno)
    				{
    					printf("Customer's ID: %d.\n",cust1.custID);
    					printf("Customer's Name: %c.\n",cust1.custName);
    					printf("Customer's Address: %c.\n",cust1.custAddress);
    					printf("Customer's Birthday: %c.\n", cust1.custBday);
    					printf("Customer's Last Product Bought: %c.\n",cust1.custProdPrice);
    					printf("Date bought: %c.\n",cust1.custDateProdBought);
    					printf("Product's Price: %c.\n",cust1.custProdPrice);
    					printf("\n");
    
    				}
    			}
    			break;
    	}
    }
    
    
    void recordCount(c c1[30], int *count)
    {
    	addRecord(c1);
    	count =0;
    	count++;
    }
    
    void sortRecord(c c1[30], int size)
    {
    	int x, y;
            c temp;
    	for(x=0 ; x < size - 1 ; x++)
    	{
    		for(y=0; y < size - 1 ; y++)
    		{
    			if(strcmp(c1[y].custName, c1[y+1].custName) > 0)
    			{
    				temp = c1[y];
    				c1[y]= c1[y+1];
    				c1[y+1]= temp;
    			}
    		}
    	}
    }
    
    
    void exit1()
    {
    	system("cls");
    	printf("Goodbye!");
    }
    int main()
    {
    	c c1[30];
    	int a,i;
    	char ch;
    		
    	printf("Creating file...\n");
    	createNew();
    
    	printf("Press enter to continue...");
    	getch();
    
    	system("cls");
    
    	do{
    		printf("\nCustomer's Information Data.\n\n");
    		printf("What do you want to do?\n");
    		printf("[1] Add new Customer Information.\n");
    		printf("[2] Update a Customer Information.\n");
    		printf("[3] Display list of record.\n");
    		printf("[4] Search a record.\n");
    		printf("[5] Exit.\n\n");
    
    		printf("\n\n");
    
    		recordCount(c1, &i);
    		printf("\n\n");
    	
    		sortRecord(c1,i);
    		do
    		{
    			scanf("%d",&a);
                            (void) getchar(); //clear the newline from the input stream
    			switch(a)
    			{
    				case 1: addRecord(c1);break;
    				case 2: updateRecord();break;
    				case 3: displayFile();break;
    				case 4: searchFile();break;
    				case 5: exit1();
    				default: printf("Invalid Choice!");
    			}
    		}
    		while (a<0 && a>7);
    
    		printf("\nDo you want to try again(Y/N)? :");
    		scanf("%c", &ch);
    	}	
    	while(ch=='y'|| ch=='Y');	
    
            return 0;
    }
    It's not good code yet, but it will compile at least. c1 is the name of the array now.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Wow. Maybe my code wasn't that good. But it's ok.
    Me and my classmates tried to solve the problem together. The only problem we had was the updateRecord function. We tried several ways, but none of them seem to work. :P Otherwise, it's a good program. If you want to look at the program, tell me. But I'm not forcing you guys :3 You are already helping me a lot.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by echzee View Post
    Wow. Maybe my code wasn't that good. But it's ok.
    Me and my classmates tried to solve the problem together. The only problem we had was the updateRecord function. We tried several ways, but none of them seem to work. :P Otherwise, it's a good program. If you want to look at the program, tell me. But I'm not forcing you guys :3 You are already helping me a lot.
    Test it out and see what's not working (if anything. I REALLY don't know). If you change anything from the above program, post it up. I'll take a further look at it.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    So here's the code me and my classmates worked together:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    typedef struct customer c;
    
    struct customer
    {	int cosID;
    	char name[30];
    	char address[30];
    	char gender[10];
    	char birthday[20];
    	char prodBought[30];
    	char dateProdbought[20];
    	int prodPrice;
    };
    
    int dup(int n, customer c[20], int id)//to duplicate how many files you want to output
    {
         int j;
    
         if(n == 0)
             return 1;
    
         for(j=0;j<n;j++)
         {
              if(c[j].cosID==id)
                return 0;
         }
         return 1;
        
    }
    
    
    
    void addRecord(int h, customer c[20])//to add a record of a costumer
    {
        int check;
         
                 system("cls");
                fflush(stdin);
                do
                {
                    printf("\nCustomer ID      : ");
                    scanf("%d",&c[h].cosID);
                    if((check = dup(h, c,c[h].cosID)) == 0)
                    {
                        printf("\n\nCustomer's record exists.\n\n");
                        continue;
                    }
                    printf("Name               : ");
                    fflush(stdin);
                    gets(c[h].name);
                    printf("Birthdate          : ");
                    fflush(stdin);
                    gets(c[h].birthday);
    				printf("Gender             : ");
                    fflush(stdin);
                    gets(c[h].gender);
    				printf("Address            : ");
    				fflush(stdin);
    				gets(c[h].address);
    
                    printf("Product bought     : ");
                    fflush(stdin);
                    gets(c[h].prodBought);
    				printf("Date product bought: ");
                    fflush(stdin);
    				gets(c[h].dateProdbought);
                    printf("Product Price      : ");
    				scanf("%d",&c[h].prodPrice);
    
    
                    printf("\nCostumer record successfully saved!");
                }while(check==0);
    
    }
    
    
    void sortRecord(customer c[], int size)//to make it organized by name
    {
          int x, y;
          customer temp;
          for(x=0 ; x < size - 1 ; x++)
          {
           for(y=0 ; y < size - 1 ; y++)
           {
                
                if(strcmp(c[y].name, c[y+1].name) > 0)
                {
                    temp = c[y];
                    c[y]= c[y+1];
                    c[y+1]= temp;
                }
         
         
          }
          }
               
    }
    
    
    void searchRecord(customer y[], int cusID, int size)//to search a record
    {     
          for(int a = 0; a < size; a++)
          {
              if(y[a].cosID== cusID)
              {
    			printf("\n¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\n");
                printf("\nCustomer ID    : %d",y[a].cosID);
                printf("\nNAME           : %s",y[a].name);
                printf("\nBIRTH DATE     : %s",y[a].birthday);
    			printf("\nADDRESS        : %s",y[a].address);
                printf("\nPRODUCT BOUGHT : %s",y[a].prodBought);
                printf("\nGENDER         : %s",y[a].gender);
                printf("\nDATE BOUGHT    : %s",y[a].dateProdbought);
                printf("\nPRODUCT PRICE  : %d",y[a].prodPrice);
    			
                break;
              }
          }
    }
    
    void displayFile(customer y[], int x)//to display
    {
        system("cls");
        for(int z = 0; z < x; z++)
        {
       	   		printf("\n¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\n");
                printf("\nCustomer ID    : %d",y[z].cosID);
                printf("\nNAME           : %s",y[z].name);
                printf("\nBIRTH DATE     : %s",y[z].birthday);
    			printf("\nADDRESS        : %s",y[z].address);
                printf("\nPRODUCT BOUGHT : %s",y[z].prodBought);
                printf("\nGENDER         : %s",y[z].gender);
                printf("\nDATE BOUGHT    : %s",y[z].dateProdbought);
                printf("\nPRODUCT PRICCE : %d",y[z].prodPrice);
    			
                break;
         }
      
    }
    
    char menu()//menu
    {
          printf("\n\n\t\t\to.O O.o CUSTOMER'S RECORD o.O O.o");
          printf("\n\n\n\n\t\t\tN - Customer Account");
    	  printf("\n\t\t\tB - Search details");
    	  printf("\n\t\t\tC - Show Details");
    	  printf("\n\t\t\tO - Count File");
    	  printf("\n\t\t\t\tE - Edit Account");
    	  printf("\n\t\t\tL - Load Information");
    	  printf("\n\t\t\tS - Save");
    	  printf("\n\t\t\tQ - Quit");
    
    
         return toupper(getch());
    }
    
    
    void updateRecord(customer c[],int h)
    {
    	FILE *ct;
    	
    	char filename[30];
    	int check;
    	
    	  printf("Customer's ID: ");
    	  scanf("%d",&c[h].cosID);
    
    	if((check =dup(h,c,c[h].cosID))=0)
    	{
    		ct=fopen(filename,"r+");
    	
    	}
    
    	system("pause>null");
    
        
    }
    
    void save(customer c[], int i)//to save your files
    {
        FILE *ct;
        char cusname[30];
    
        printf("Input Customer's name to save: ");
        gets(cusname);
    
          if((ct = fopen(cusname,"w")) == NULL)
          {
              printf("FILE NOT AVAILABLE");
             return;
          }
          fwrite(c, sizeof(customer)* i, 1, ct);
          fclose(ct);
          printf("File Successfully Save.!!");
    }
       
    
    void load(customer c[], int *i)// to load your files into program
    {
        FILE *ct;
        char cusname[30];
    
        printf("Input customer's name to load: ");
        scanf("%s", cusname);
    
          if((ct = fopen(cusname,"r")) == NULL)
          {
              printf("FILE NOT AVAILABLE");
             return;
          }
          for(int y= 0; !feof(ct); y++)
          {
            fread(&c[y], sizeof(customer), 1, ct);
          }
          *i = y - 1;
               
          fclose(ct);
          printf("\n\nSuccessfully Loaded.!!");
    
            system("pause >nul");
    }
    
    
    void countRecord(int count)//to load files
    {
        count = 0;
        count++;
    }
    
    int main()
    {
          customer c[20];
          char choice;
          int cusID,h=0,count=0;
    
     
             
            
           do{
    
                system("cls");
                sortRecord(c, h);
                choice = menu();
                switch(choice)
                {
    
                case 'N':
                        addRecord(h,c);
                        h++;
                        system("pause >nul");
                        break;
    		
                case 'B' :
                         system("cls");
                         fflush(stdin);
                         printf("Search Customer ID:");
                         scanf("%d",&cusID);
                         searchRecord(c,cusID,h);
                         system("pause >nul");
                         break;
                case 'C':
                    displayFile(c, h);
                    system("pause >nul");
                    break;
                
                case 'S':
                    system("cls");
                    save(c,h);
                    break;
    
                case 'L':
                    system("cls");
                    load(c,&h);
                    break;
                case 'O':
                    system("cls");
                    countRecord(h);
                    printf("There are %d records in your file",h);
                    system("pause >nul");
                    break;
    
    			case 'E': 
    				system("cls");
    				updateRecord(c, h);
    				break;
    
                
                case 'Q' :
    					system("cls");
    					printf("\n\n\t\t\tGoodbye!\n\t\t\tCome again!:)\n\n\n\n\n\t\t\t");
    					getch();
                        return 0;
    
    
                }
                      }while(1);
     
       
    
       getch();
    
    return 0;
    }
    The updateFunction is lacking. It's hard working on a problem you haven't discussed about.


    PS:
    We use Microsoft Visual C++ 6.0 as our compiler.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Several problems:

    1) Every place you use the word "customer", after the typedef of customer to c, is wrong. c is an alias for "customer", and since c IS an alias for customer, you should change the name of the array to something besides c -- maybe c1 or ANYTHING else, but not c.

    2) In your update function, you don't write out any data to the file. Look at the version I posted - see the fwrite in there? You have to a) find just the right place in the file and b) write out the record you change.

    3) In search record, you're not saving any info from your search. You need to save the value of 'a'. Why not return it after you break out of the for loop? Without that value, your search is OK to look at, but you can't use it with updatefunction to a good purpose.

    In just a few hours you and your classmates have re-shaped the program a lot. Once it compiles, begin testing it, function by function. I don't have time to tweak up this version. I thought I was working on the current version, but obviously, not.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Thank you so much for your effort, Adak.
    You really helped me a lot!
    I'm really sorry if I wasted your time.
    I really can't express how much I appreciate your effort
    Last edited by echzee; 03-17-2011 at 10:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 02:26 AM

Tags for this Thread