Jump to content

Problems with file input/output

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
extrem3

extrem3

    Newbie

  • Members
  • Pip
  • 1 posts
I am working on an assignment (C Programming).
Firstly, please have a look at my question and the coding:

Question(unable to post image and link due to insufficient post count, sorry):

h t t p ://img198.imageshack.us/img198/9537/questionqmc.jpg

(remove the spaces)

Problem:
1. How to delete part of the entry from the address book?
2. Problem retreving address as string from file, if my address contain spaces, it will only read the 1st value (eg. Road 32, it will only read Road, then 32 will assign to the next variable).
3. How to sort the address by name or last time when they are being display?
4. How to compare string and how to change string to upper case (fail to use toupper(VARIABLE))?
5. Is there any weakness of my code and can be improved?

Coding:



...


int main(void)


{

	FILE* ABook;

	int cont=0,status,content,closeresult;

	char fname[30],lname[30],gender[10],addr[20],street[30],city[30],zcode[20],phone[20],email[50];


	greeting();

	content=selection();

	do

	{

		if(cont==2)

			content=selection();

	  switch(content)

	  {

		case 1:

			do

			{

			ABook=fopen("Add_Book.txt","r");

			ofile(ABook);

			for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))

				printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);

			closeresult=fclose(ABook);

			cfile(ABook,closeresult);

			cont=conti();

			if(cont==3)

			{

				end();

				exit(301);

			}

			}while(cont==1);

			break;


		case 2:

			do

			{

			ABook=fopen("Add_Book.txt","a");

		    ofile(ABook);

			printf("\n");

            insert(ABook);

		    closeresult=fclose(ABook);

	        cfile(ABook,closeresult);

            cont=conti(cont);

			if(cont==3)

			{

				end();

				exit(301);

			}

			}while(cont==1);

			break;


		case 3:

			do

			{

			ABook=fopen("Add_Book.txt","r");

			ofile(ABook);

            search(ABook);

	        cfile(ABook,closeresult);

            cont=conti();

			if(cont==3)

			{

				end();

				exit(301);

			}

			}while(cont==1);

			break;


		case 4:

			do

			{

			ABook=fopen("Add_Book.txt","r");

			ofile(ABook);

			printf("\nin process\n");

	        cfile(ABook,closeresult);

            cont=conti();

			if(cont==3)

			{

				end();

				exit(301);

			}

			}while(cont==1);

			break;


		case 5:

			end();

			exit(301);

			break;

	  }

	}while(cont==2);

	return 0;


}


......


void insert(FILE* ABook)

{

	int z;

        printf("First Name: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("Last Name: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

			printf("Gender[M/F]: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("Address: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("Street: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("City: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("Zip Code: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

		printf("Phone Number: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

			printf("Email Address: ");

		while((z = getchar()) != EOF)

			fputc(z, ABook);

}


void search(FILE* ABook)

{

	int search,status,display=0;

	char fname[30],lname[30],gender[10],addr[20],street[30],city[30],zcode[20],phone[20],email[50],surname[30],code[20],pnum[20];

	printf("\nSearch by:");

	printf("\n1 => Surname:");

	printf("\n2 => Zip Code:");

	printf("\n3 => Phone Number:");

	printf("\n[1/2/3]: ");

	scanf("%d",&search);

	while(search!=1&&search!=2&&search!=3)

	{

		printf("\nError!!! Please enter correct code!!!");

		printf("\nSearch by:");

		printf("\n1 => Surname:");

		printf("\n2 => Zip Code:");

		printf("\n3 => Phone Number:");

		printf("\n[1/2/3]: ");

		scanf("%d",&search);

	}

	if(search==1)

	{

		printf("Enter the surname: ");

		scanf("%s",surname);

		for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))

			if(strcmp(surname,lname)==0)

			{

				printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);

				display++;

			}

			if(display==0)

				printf("Surname (%s) cannot be found in the Address book!!!\n",surname);

	}

	else if(search==2)

	{

		printf("Enter the Zip Code: ");

		scanf("%s",code);

		for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))

			if(strcmp(code,zcode)==0)

			{

				printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);

				display++;

			}

			if(display==0)

				printf("Zip Code (%s) cannot be found in the Address book!!!\n",code);

	}

	else if(search==3)

	{

		printf("Enter the Phone Number: ");

		scanf("%s",pnum);

		for(status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,zcode,phone,email);status != EOF; status=fscanf(ABook,"\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",fname,lname,gender,addr,street,city,&zcode,&phone,email))

			if(strcmp(pnum,phone)==0)

			{

				printf("\nFirst Name: %s\nLast Name: %s\nGender: %s\nAddress: %s\nStreet: %s\nCity: %s\nZip Code: %s\nPhone Number: %s\nEmail Address: %s\n",fname,lname,gender,addr,street,city,zcode,phone,email);

				display++;

			}

			if(display==0)

				printf("Phone Number (%s) cannot be found in the Address book!!!\n",pnum);

	}

}


......

}



#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I will give U my logic.. Have U studied a subject called Data Structures.... Have U studied Binary tree.... Better try using that...

define a structure like
struct Abook {
        char lname[100];
        char fname[100];
        Char gender[10];
        char addr[20];
        char street[30];
        char city[30];
        int zcode;
        int phone;
        char email[50];
        struct Abook left;
        struct Abook right;
};

I think, U are saving all the entries directly into the file.. try saving them in the memory first.. When U r finished, save those entries into the file and close the file.... Because It takes much time when U r accessing the file for each and every time...

Now create a binary tree depending on the lastname.... Inorder traversal will give U the sort list of address book...
Whenever U want to add an entry, try using malloc(), and for deletion use free()

I don't know whether there is an std library function to change string to upper case, but i will give U an idea... Get the ASCII value of each character of the string... Subract it by 32 and form the string for the new ASCII value... I guess This might work....

When U open a File, read the file and add the entries into the tree, and save in the same manner into the file...

I hope this helps U in someway....

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts

Quote

I don't know whether there is an std library function to change string to upper case, but i will give U an idea... Get the ASCII value of each character of the string... Subract it by 32 and form the string for the new ASCII value... I guess This might work....

Don't do that, it won't always work.

#include <ctype.h>

void strtoupper(char *str)
{
    size_t i = 0;
    while(str[i] != '\0')
    {
        if(isalpha(str[i]))
            str[i] = toupper(str[i]);
        ++i;
    }
}

If you need help creating the binary tree in pure C, I can help you with that.

Comments on struct below.
struct _Abook {
        char lname[100];
        char fname[100];
        char gender[10];
        char addr[20];
        char street[30];
        char city[30];
        int zcode;
        int phone;  /*I would change this to a char[16]*/
        char email[50];
        _Abook *left;  /*note these *must* be pointers for this to work.*/
        _Abook *right;
};
typedef    struct _Abook    Abook;

sudo rm -rf /

#4
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
And he should not write the pointers into the file because they are just to link structures in memory, so it's useless to write them on file.

So something like this should be done:

Quote


int write_file(const Abook* abook, const char* path)
{
FILE* fp = fopen(path, "w");

if(!fp)
{
return 0; // Error indication
}

if(fwrite(abook, sizeof(Abook) - sizeof(Abook*) * 2, 1, fp) != 1)
{
return 0;
}

fclose(fp);

return 1; // Success
}


@veda87

What's that thing with U's ?

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I'm guessing veda87 is Dutch (or Dutch-speaking). That's the formal second-person pronoun. (Yes, it is sometimes capitalized in Dutch. Just like Sie is in German.)
sudo rm -rf /