Thread: writing to file

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    19

    writing to file

    Hey all, Can someone help me with writing to a file? No matter how much I try to understand and get it to work i just can't do it... My program gets the clients details when the 'add client' option is chosen and puts them in the structure. That all seems to work, but how do I write that to a file, and how do I allow the user to input a long list of clients?

    Any help would be really appreciated, heres the code I wrote so far:
    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct 
    {
    	char first_name[15];
    	char second_name[25];
    	char pharmacy_address[25];
    	char pharmacy_postcode[9];
    	char pharmacy_tel[13];
    	char dealing_with[15]; /* person within company dealing with client */
    	char bank[30];
    	char solicitors[30];
    	char accountants[30];
    	char notes[50];
    } CLIENT;
    
    int f_menu ();
    void f_create ();
    CLIENT f_add();
    void f_edit ();
    void f_search ();
    void f_displayall ();
    char* f_validate_string_length ( int max_length );
    
    void main()
    {
    	
    	int menu = 0;
    	CLIENT customer;
    
    	menu = f_menu ();
    
    	do
    	{
    
    		switch (menu)
    		{
    			case 1 :
    				 f_add ();
    				break;
    			case 2 :
    				f_edit ();
    				break;
    			case 3 :
    				f_search ();
    				break;
    			case 4 :
    				f_displayall ();
    				break;
    			case 5 :
    				printf("Exiting Program\n\n");
    				break;
    		}
    	} while (menu != 5);
    
    }
    
    int f_menu ()
    {
    	int menu = 0;
    
    	printf("\n\n  Medical Finance Ltd\n\n");
    	printf("  1. Add Client\n  2. Edit Client\n  3. Search Client\n  4.Display All Clients\n  5.Exit\n ");
    	printf("\nEnter choice: ");
    
    	do
    	{
    		scanf("%d", &menu);
    		fflush(stdin);
    
    		if((menu < 1 ) || (menu > menu))
    		{
    			printf("Invalid choice. Try again: ");
    		}
    
    	} while ((menu < 1 ) || (menu > menu));
    
    	return (menu);
    }
    
    void f_create ()
    {
    
    }
    
    char* f_validate_string_length ( int max_length )
    {
    	char input[200];
    	
    	do
    	{
    		gets(input);
    		if ( strlen(input) > max_length)
    		{
    			printf("invalid input (too many characters). Please re-enter: ");
    		}
    	} while( strlen(input) > max_length);
    
    	return(input);
    }
    
    
    
    CLIENT f_add ()
    {
    	CLIENT customer;
    
    	printf("Enter client first name : ");	
    	strcpy(customer.first_name,f_validate_string_length ( 14 ));
    
    	printf("Enter second name : ");
    	strcpy(customer.second_name,f_validate_string_length ( 24 ));
    
    	printf("Enter pharmacy address : ");
    	strcpy(customer.pharmacy_address,f_validate_string_length ( 24 ));
    
    	printf("Enter pharmacy postcode : ");
    	strcpy(customer.pharmacy_postcode,f_validate_string_length ( 8 ));
    
    	printf("Enter pharmacy telephone number : ");
    	strcpy(customer.pharmacy_tel,f_validate_string_length ( 12 ));
    
    	printf("Enter name of person dealing with : ");
    	strcpy(customer.dealing_with,f_validate_string_length ( 14 ));
    
    	printf("Enter name of clients bank : ");
    	strcpy(customer.bank,f_validate_string_length ( 29 ));
    
    	printf("Enter clients solicitors : ");
    	strcpy(customer.solicitors,f_validate_string_length ( 29 ));
    
    	printf("Enter clients accountants : ");
    	strcpy(customer.accountants,f_validate_string_length ( 29 ));
    
    	printf("Enter any notes reletive to client : ");
    	strcpy(customer.notes,f_validate_string_length ( 49 ));
    
    	return (customer);
    }
    
    void f_edit ()
    {
    }
    
    void f_search ()
    {
    }
    
    void f_displayall ()
    {
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by renvaar
    That all seems to work, but how do I write that to a file
    Open a file, call fwrite of fprintf to write the data to the file, close the file. You have to decide where in your code to do this, perhaps in a write_to_file function at the end of your program prior to exiting where you loop through all your objects and write each one out to the file. Or, perhaps in the add function where each new CLIENT struct you fill can be written right after it's entered by the user. Presumably you're going to have a function to read data in from this file somewhere towards the beginning of the program so it loads the data into an array at program startup?


    Quote Originally Posted by renvaar
    and how do I allow the user to input a long list of clients?
    You currently call you menu function once and then repeatedly loop performing that one option... once the user presses 1, your code would continuously call the "add" function over and over again. You need to put your call to the menu function inside the loop so that the user gets to pick a new option each time through the loop. You probably also want an array of CLIENT objects/structs and not just a single object. Your add function is returning an object but in the loop where you call this function you are not doing anything with the return value.


    Code:
    void main()
    People tend to be picky about this topic. main should return int not void.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    19
    I called the function in the loop so when the details are added the menu is displayed again, so I guess it would make sense to write to the file after each time the user has entered a new client.... not sure if I can code it but I'll have a go...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    If data in the file only needs to be read by your app use fwrite. This is a binary write and will allow you to write the data out in blocks of CLIENT structs. Then you can use fread and read it in in blocks of CLIENT structs. This will be your quickest and most efficient way. If it it needs to be readable by notepad or another app then you'll need to write it out in characters using the fprintf route, which will require a little more trickery with parsing the data on both the writes and reads.
    Last edited by mayday; 03-18-2010 at 02:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM