GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2007, 02:15
molatelo molatelo is offline
New Member
 
Join Date: Apr 2007
Posts: 3
molatelo is on a distinguished road

Help with creating files in c


Hi,
Can someone help me to Write a C program that will do the following:

• Read in a number of records of your friends’ names and birthdays (string with format <mmdd>). Save it on a binary file. Each record (struct) must also have a record number (sequential and starting at 1) that is generated automatically by the program and stored in the first field of the struct.
• Create two index files, i.e. a file consisting of a name field and a record number field. The file must be sorted (use a sorting algorithm) according to the name field. Do the same for the birthday field. This process must take place automatically after the names have been entered.
• Use the index files to find the birthday of a friend if the name is entered, or the name if a date is entered, or a list of every birthday in a specific month.
• You must also be able to update the file (go to the end and add records).
• All the above options must be displayed as a menu and implemented by means of appropriate functions. Also give appropriate error messages (e.g. if the file does not exist.

Here is my code:
CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define NAME_SIZE  20
#define STRSZ  6
#define MAX_FRIENDS 4

typedef struct {
	char name[NAME_SIZE];
	char birthday[STRSZ];
} friend_t;

typedef struct s {
	struct s *next_record;
	friend_t friend;
}record_t;
record_t *friends_start =NULL, *friends_last = NULL;

int read_friend_record(friend_t *friend);
int store_friend_record(const friend_t friend);
friend_t next_friend(const int first);
int friend_name(const friend_t friend, const char name[]);
int save_friend_file(const char file_name[]);
int string_read(const char prompt[], char string[], const int size);
int find_friend(friend_t *friend, char myfriend_name[]);
void print_friend_record(const friend_t friend);
void print_all_friend(void);

int main()
{

friend_t friend;
	char myfriend_name[NAME_SIZE];
	char filename[NAME_SIZE];
	FILE *out;


	 	 while(read_friend_record(&friend)) {
	 if(store_friend_record(friend))
			printf("\n Record on '%s' stored ", friend.name);
		else
			printf("\n\a  Failed to store '%s' ", friend.name);
	}

			printf("\n\n Enter name of file to save friend records ");
	scanf("%s", filename);
	out = fopen(filename, "wb");
	if(out==NULL)
		printf("\n Cannot open file for output");
		if(save_friend_file(filename))
			printf("\n Record on '%s' saved ", friend.name);
		else
			printf("\n\a  Failed to save '%s' \n", friend.name);

	fclose(out);

				while(string_read("\n\nFind friend name?", myfriend_name, NAME_SIZE)) {
			if(find_friend(&friend, myfriend_name))
				printf("\n  '%s' data found", myfriend_name);
			else
				printf("\n  '%s' data not found", myfriend_name);
		}

			print_all_friend();

return 0;
}

int read_friend_record(friend_t *friend)
{
	printf("\n\nEnter record on next friend \n");
	if(string_read("friend name?", (*friend).name, NAME_SIZE))
		if(string_read("friend birthday?", (*friend).birthday, STRSZ))
			return 1;
		return 0;
		}

			int store_friend_record(const friend_t friend)
		{
			int num_of_friends = 0;
			friend_t friends[MAX_FRIENDS];
			record_t *new_record;


			if((new_record = (record_t*) calloc(sizeof(record_t), 1)) != NULL)
			{
				if(friends_start==NULL)
					friends_start = new_record;
				else
					friends_last->next_record = new_record;
				friends_last = new_record;
				new_record->next_record = NULL;
			}
			else
			{
				printf("\n\a creation of new friend record failed \n ");
				return 0;
			};

			new_record->friend = friend;
			return 1;
		}

		friend_t next_friend(const int first)
		{
			record_t *friends = NULL;
			friend_t friend = {""};

			if(first)
				friends = friends_start;
			if(friends != NULL)
			{

				friend = friends->friend;
			     friends = friends->next_record;
			}
			return friend;
		}

		int friend_name(const friend_t friend, const char name[])
		{
			return(!strcmp(friend.name, name));
		}

		int save_friend_file(const char file_name[])
		{
			FILE *out;
			friend_t friend;

			printf("\n\nSave friend records to file'%s' ", file_name);
			if((out = fopen(file_name, "wb"))==NULL)
				printf("\nUnable to open friend records file");

			friend = next_friend(1);
			while(!friend_name(friend, "")) {
				fwrite(&friend, sizeof(MAX_FRIENDS), 1, out);
				friend = next_friend(0);
			}
			fclose(out);
			return 1;
		}

		int string_read(const char prompt[], char string[], const int size)
		{
			int index = 0, ch;

			printf("%s", prompt);
			while(((ch=getchar()) !='\n') && (ch!=EOF)) {
				if(isprint(ch) && (index<size-1))
				   string[index++] = (char) ch;
				string[index] = '\0';
			}
				return index;
			}

			int find_friend(friend_t *friend, char myfriend_name[])
			{
				*friend = next_friend(1);
				while(!friend_name(*friend, "")) {

					if(friend_name(*friend, myfriend_name))
						return 1;
					*friend = next_friend(0);
			}
				return 0;
			}

			void print_friend_record(const friend_t friend)
			{
				printf("\n %-*s  %-*s ", NAME_SIZE, friend.name, STRSZ, friend.birthday);
			}

			void print_all_friend(void)
			{
				friend_t friend;

				printf("\n\n All friends record\n %-*s  %-*s ", NAME_SIZE, friend.name, STRSZ, friend.birthday);

				friend = next_friend(1);
				while(!friend_name(friend, ""))
				{
					print_friend_record(friend);
					friend = next_friend(0);\
				}
			}
  #2  
Old 28-Apr-2007, 07:20
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 514
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: Help with creating files in c


Wow! You've done all that already?
Does it compile and run?
What DOESN'T it do that you need help with?

In your own words, send back an outline of what your present code does.
Start with the first thing in main() , end with the last, list everything that happens in between to the best of your ability.
++Howard;
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating password protected files dabigmooish C++ Forum 5 12-Jan-2007 06:43
issues compiling .h and .cpp files with Dev C++ iceman2006 C++ Forum 9 17-May-2006 19:32
"HTML Help Workshop" to creating help files shinyhui MS Visual C++ / MFC Forum 0 08-Aug-2005 03:52
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:52.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.