Writing to a Binary File

This is a discussion on Writing to a Binary File within the C Programming forums, part of the General Programming Boards category; Ok this thread is related to the other thread posted by someone else on this message board in regards to ...

  1. #1
    Lah
    Lah is offline
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    Writing to a Binary File

    Ok this thread is related to the other thread posted by someone else on this message board in regards to a C program. So dude if you've got any troubles u might be able to find some answers in my program. Anyhow with this program it needs to be able to
    1.Modify patient record
    2.Show patient record
    3.Process Job
    q.Quit from the system

    So far i can show patient record and quit the system. What i cant do it modify patient record and once i can do that i can proceed to the process job section of the program.
    What i need is someone to show me how to modify the name and address of a particular patient. This will be carried out in the ModifyPatient() function.

    Here is my code so far, obviously some of it is absolutely useless and doesnt work. But most of it works and compiles.



    Code:
    #include <stdio.h>
    #include <time.h>
    
    typedef struct
    {
    	char fName[21];
    	char gName[21];
    } name_struct;
    
    typedef struct
    {
    	char street[21];
    	char suburb[16];
    	char state[4];
    	int postcode;
    } address_struct;
    
    typedef struct
    {
    	char id[5];
    	name_struct name;
    	address_struct address;
    	double balance ;
    	char lastUpdate[11];
    } patient_struct;
    
    /*typedef struct
    {
    	int jobCode;
    	char description[21];
    	double fee;
    }fee_struct;*/
    
    void showMenu();
    void modifyPatient();
    void showPatient();
    void processFile();
    void getDate(char *);
    //void FeeSchedule(fee_struct fee[30]);
    void displayPatient(patient_struct);
    void getPatient(patient_struct patient);
    void modifyfName();
    void modifygName();
    void modifyStreet();
    void modifySuburb();
    void modifyState();
    void modifyPostCode();
    
    char today[11];
    
    
    
    main()
    {
    	//fee_struct feeSchedule[30];
    	char option ='0';
    	getDate(today);
    	//getFeeSchedule(feeSchedule);
    	while(option!='q'||option!='Q')
    	{
    		showMenu();
    		option=getchar();getchar();
    		switch(option)
    		{
    			case '1':modifyPatient();
    					 break;
    			case '2':showPatient();
    					 break;
    			case '3':processFile();
    					 break;
    			case 'q':
    			case 'Q':exit(0);
    		}
    	}
    }
    
    void getDate(char *today)
    {
    	time_t x;
    	struct tm *t;
    	time(&x);
    
    	t = localtime(&x);
    
    	sprintf(today,"%d/%d/%d\n", t->tm_mday, t->tm_mon + 1, t->tm_year + 1900);
    	puts(today);
    }
    
    /*void FeeSchedule(fee_struct fee[30])
    {
    	//Here you
    	//1.open the text file
    	//2.read the records and put them in the array (week8-9)
    }*/
    
    void showMenu()
    {
    	printf("\n\n");
    	printf("Dr Stevenson Dental Surgery\n");
    	printf("Patient System\n\n");
    	printf("1. Modify patient record\n");
    	printf("2. Show patient record\n");
    	printf("3. Process work file\n");
    	printf("q. Quit the system\n");
    	printf(" Choose 1,2,3 or q: ");
    }
    
    void modifyPatient()
    {
    	FILE *file;
    	int i;
    	char option ='0';
    	char patientId[5];
    	patient_struct patient;
    	printf("Enter patient id: ");
    	gets(patientId);
    	file=fopen("patient.dat","rb");
    	fread(&patient, sizeof(patient), 1,file);
    	while(!feof(file))
    	{
    	if (strcmp(patientId,patient.id)==0)
    	while(option!='q'||option!='Q')
    	{
    	getPatient(patient);
    	option=getchar();getchar();
    	switch(option)
    		{
    			case '1':modifyfName();
    					 break;
    			case '2':modifygName();
    					 break;
    			case '3':modifyStreet();
    					 break;
    			case '4':modifySuburb();
    					 break;
    			case '5':modifyState();
    					 break;
    			case '6':modifyPostCode();
    			case 'q':
    			case 'Q': exit(0);
    		}
    	}
    	fread(&patient, sizeof(patient), 1,file);
    }//end of file
    	fclose(file);
    }
    
    void modifyfName()
    {
    //printf("not yet done\n");
    FILE *file;
    patient_struct patient;
    file=fopen("patient.dat","ab");
    fwrite(&patient,sizeof(patient_struct),1,file);
    printf("Enter new family name: ");
    gets(patient.name.gName);
    }
    
    void modifygName()
    {
    printf("not yet done\n");
    }
    
    void modifyStreet()
    {
    printf("not yet done\n");
    }
    
    void modifySuburb()
    {
    printf("not yet done\n");
    }
    
    void modifyState()
    {
    printf("not yet done\n");
    }
    
    void modifyPostCode()
    {
    printf("not yet done\n");
    }
    
    void getPatient(patient_struct patient)
    {
    	printf("\n");
    	printf("  Patient id     	: %s \n", patient.id);
    	printf("1.Family name	 	: %s \n", patient.name.gName);
    	printf("2.Given name	 	: %s \n", patient.name.fName);
    	printf("3.Street & Number	: %s \n", patient.address.street);
    	printf("4.Suburb		: %s \n", patient.address.suburb);
    	printf("5.State			: %s \n", patient.address.state);
    	printf("6.Postcode		: %d \n", patient.address.postcode);
    	printf("  Balance		: %.2lf\n", patient.balance);
    	printf("  Last update		: %s\n", patient.lastUpdate);
    	printf("q.Go back to menu\n");
    }
    
    void showPatient()
    {
    	//get patient id
    	FILE *file;
    	char patientId[5];
    	patient_struct patient;
    	printf("Enter patient id: ");
    	gets(patientId);
    	file=fopen("patient.dat","rb");
    	fread(&patient,sizeof(patient_struct),1,file);
    	while(!feof(file))
    	{
    	if(strcmp(patientId,patient.id)==0)
    	{
    		displayPatient(patient);
    		break;
    	}
    	fread(&patient,sizeof(patient_struct),1,file);
    }//end of file
    fclose(file);
    }
    
    void processFile()
    {
    	printf("not yet done\n");
    }
    
    void displayPatient(patient_struct patient)
    {
    	printf("\n");
    	printf("Patient id     : %s \n", patient.id);
    	printf("Patient name   : %s %s \n", patient.name.gName, patient.name.fName);
    	printf("Patient address: %s \n", patient.address.street);
    	printf("                 %s %s %d\n", patient.address.suburb,
    	         patient.address.state, patient.address.postcode);
    	printf("Balance        : %.2lf \n", patient.balance);
    	printf("Last update    : %s\n", patient.lastUpdate);
    }



    The link to the binary file:
    http://www.uow.edu.au/~yang/buss111/files/patient.dat

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    You don't write to the file when you've modified the record.

    Code:
    fseek( file, ((int) sizeof patient) * -1, SEEK_CUR );  // step back over the record we just read
    fwrite( &patient, sizeof patient, 1, file );
    fflush( file );  // must fflush() between writes and reads
    > file=fopen("patient.dat","rb");
    Try rb+
    for an update binary file

    > char today[11];
    It is pointless to have this as a global variable

    > gets(patientId);
    gets() is unsafe - period
    Read the FAQ to find out more

    > fread(&patient, sizeof(patient), 1,file);
    > while(!feof(file))
    By doing this
    &nbsp;&nbsp;&nbsp;while ( fread(&patient, sizeof(patient), 1,file) == 1 )
    you remove the need for a fread() inside the loop as well - one thing, done correctly, in the right place.
    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. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 2
    Last Post: 05-09-2008, 08:27 AM
  3. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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