1
void Search()
{
  
 char Find[100],*ptr;
 int Found=0, ch;
 if((Contactbook=fopen("Contactbook.txt","r"))==NULL)
  printf("----The File Is Empty----\n");
 else
 {
  system("clear");
  while ((ch = getchar()) != '\n' && ch != EOF);
  printf("Enter Name to search:\n");
   
  fgets(Find, sizeof Find, stdin);
  if((ptr = strchr(Find, '\n')) != NULL)
	*ptr = '\0'; 
	
  fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
  if(strcmp(Find,contactbook.names)==0)
  Found=1;
  
  if(Found)
  {
    system("clear");
    printf("Name: %s\n",contactbook.names);
    printf("Birthday: %s\n",contactbook.birthday);
    printf("Handphone: %s\n",contactbook.hp);
    printf("Address : %s\n",contactbook.address);
    printf("Occupation: %s\n",contactbook.occupation);
      
  } 
  else if(!Found)
  {
    printf("SORRY FILE NOT FOUND\n");
          
  }
  fclose(Contactbook);
      
     }
}
1
void Search()
{
  
 char Find[100],*ptr;
 int Found=0, ch;
 if((Contactbook=fopen("Contactbook.txt","r"))==NULL)
  printf("----The File Is Empty----\n");
 else
 {
  system("clear");
  while ((ch = getchar()) != '\n' && ch != EOF);
  printf("Enter Name to search:\n");
   
  fgets(Find, sizeof Find, stdin);
  if((ptr = strchr(Find, '\n')) != NULL)
	*ptr = '\0'; 
	
  fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
  if(strcmp(Find,contactbook.names)==0)
  Found=1;
  
  if(Found)
  {
    system("clear");
    printf("Name: %s\n",contactbook.names);
    printf("Birthday: %s\n",contactbook.birthday);
    printf("Handphone: %s\n",contactbook.hp);
    printf("Address : %s\n",contactbook.address);
    printf("Occupation: %s\n",contactbook.occupation);
      
  } 
  else if(!Found)
  {
    printf("SORRY FILE NOT FOUND\n");
          
  }
  fclose(Contactbook);
      
     }
}

Just a small corection. Instead of

fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
  if(strcmp(Find,contactbook.names)==0)
  Found=1;

you shold do this

while(fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation) == 5)
  {
     if(strcmp(Find,contactbook.names)==0)
     {
         Found=1;
         break;
     }
  }

Be carefull! With this method fscanf you can't afford to have whitespaces in name, birthday, phone, address and ocupation. Why? Becouse for example you say for address foo 16 then contactbook.address will be foo and in next iteration contactbook.names will be the ocupation.

1

Just a small corection. Instead of

fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
  if(strcmp(Find,contactbook.names)==0)
  Found=1;

you shold do this

while(fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation) == 5)
  {
     if(strcmp(Find,contactbook.names)==0)
     {
         Found=1;
         break;
     }
  }

Be carefull! With this method fscanf you can't afford to have whitespaces in name birthday, address etc. Why? Becouse for example you say for address foo 16 then contactbook.address will be foo and in next iteration contactbook.names will be 16.

Thanks Grunt and Andor. Now it works. Btw, what about delete function? I tried implementing the same way as search but it's still the same and also why the list function will not work after we add the input. i mean after adding the input. and then try list function, it will exit. but execute it one more time then it showed.

And for the white space any idea how to solve it? Is it i need to separate them one by one? Ok. thanks. I highly appreciate your helps and also the others.

0

I don't understand your list func. Do you want to print the whole file or just names? Becouse in your code you try to print the whole file using contactbook.names . This is bad becouse addres can hold 300 chars and name only 80.

0

I don't understand your list func. Do you want to print the whole file or just names? Becouse in your code you try to print the whole file using contactbook.names . This is bad becouse addres can hold 300 chars and name only 80.

I want to show all names that had been enter alphabatically.

0

In delete function you have a nasty error: U wrote Contacbook

void Del()
{
   int match = 0, ch;
   char Find[200], *ptr;
   system("clear");
   temp = fopen("temp.txt", "w+");
   if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
   printf("----The file is empty----\n");
   else
   {
      while ((ch = getchar()) != '\n' && ch != EOF);
      printf("Enter his/her name that you want to delete:\n");
      fgets(Find, sizeof(Find), stdin);
      if((ptr = strchr(Find, '\n')) != NULL)
         *ptr = '\0'; 
//      strip(Find, 200);
   }
 
   while(fscanf(Contactbook,"%s %s %s %s %s ",contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation) == 5)
   {
      if(strcmp(Find, contactbook.names) !=0)
         fprintf(temp,"%s\n%s\n%s\n%s\n%s\n", contactbook.names, contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
      else
      {
         match = 1;
         printf("\nHis/Her Name     : %s\n", contactbook.names);
         printf("\nBirth of Date    : %s\n", contactbook.birthday);
         printf("\nHandphone Number : %s\n", contactbook.hp);
         printf("\nAddress          : %s\n", contactbook.address);
         printf("\nOccupation       : %s\n", contactbook.occupation);
//         system("clear");
//      main();
      }
   }
   if (!match)
   {
      printf("The name does not exist\n");
      printf("or it might had been deleted\n");
//      system("clear");
   }
   fclose(Contactbook);
   fclose(temp);
   remove("Contactbook.txt");
   rename("temp.txt","Contactbook.txt"); // "Contacbook.txt" were here
}

list func

void List()
{
   Contactbook = fopen("Contactbook.txt","r");
 
   if(Contactbook != NULL)
   {
      int i = 0;
      system("clear"); 
      printf("Current Entries: \n");
      while(fgets(contactbook.names, sizeof(contactbook.names), Contactbook) != NULL)
      {
         if (i++ % 5 == 0)
            printf("%s", contactbook.names);
      }
      

      printf("End of File \n");
      system("pause");
      fclose(Contactbook);
   }
   else
   {
      printf("Contactbook.txt open FAIL!\n");
   }
}
0

Thank You so much Andor.You're the saviour at the last minute ::D hehe. Thanks to you so much and also 4 all who have helped me in doing this group work.

0

Damn mistake again. Theoretical i can overflow so list func must be modified

void List()
{
   Contactbook = fopen("Contactbook.txt","r");
 
   if(Contactbook != NULL)
   {
      int i = 0;
      system("clear"); 
      printf("Current Entries: \n");
      while(fgets(contactbook.names, sizeof(contactbook.names), Contactbook) != NULL)
      {
         if (i % 5 == 0)
         {
            printf("%s", contactbook.names);
            i = 0;
         }
         i++;
         
      }
      

      printf("End of File \n");
      system("pause");
      fclose(Contactbook);
   }
   else
   {
      printf("Contactbook.txt open FAIL!\n");
   }
}
1

Damn mistake again. Theoretical i can overflow so list func must be modified

void List()
{
   Contactbook = fopen("Contactbook.txt","r");
 
   if(Contactbook != NULL)
   {
      int i = 0;
      system("clear"); 
      printf("Current Entries: \n");
      while(fgets(contactbook.names, sizeof(contactbook.names), Contactbook) != NULL)
      {
         if (i % 5 == 0)
         {
            printf("%s", contactbook.names);
            i = 0;
         }
         i++;
 
      }
 
 
      printf("End of File \n");
      system("pause");
      fclose(Contactbook);
   }
   else
   {
      printf("Contactbook.txt open FAIL!\n");
   }
}

Oh now i get it. Although have passed it up, nevermind learning is a long life process.....:D

one more thing, i made my edit function like this ( using delete function and then add a new value into contactbook because i don't know how to overwrite the initial value containing in the previous file) Is it we have to use fseek. but how should we know the place in the array.

and you're right with white space thing. Is it we have to replace it with fgets? but if there's too many argument, how to write it. Just want to learn more.

1

<<several intervening off-topic posts were deleted>>

I marked it as solved. Since it is a relatively new thread, I don't see a need to close it.

Well the thread is not solved. Read post no 41

one more thing, i made my edit function like this ( using delete function and then add a new value into contactbook because i don't know how to overwrite the initial value containing in the previous file) Is it we have to use fseek. but how should we know the place in the array.

and you're right with white space thing. Is it we have to replace it with fgets? but if there's too many argument, how to write it. Just want to learn more.

>Is it we have to replace it with fgets?
In your case it's better to do it with fgets
>but if there's too many argument, how to write it. Just want to learn more.
Think. Now you have enough time to think some kind of solution. Search can be similar to list func but you need to change the logic of search func. If you still have a problem then post it.

1

fflush dosn't work for input streams and I think it's system("cls"); instead of system("clear");

system("cls") for windows
system("clear") for unix(knoppix)

the problem right now that i want to cocentrate at is Search function and Delete function. Anybody know. Pls help me. Urgent.......

As andor mentioned:
Why not to use fflush(stdin): FAQ > Explanations of... > Why fflush(stdin) is wrong
What to use instead: FAQ > How do I... (Level 2) > Flush the input buffer

How to clear the screen: FAQ > How do I... (Level 1) > Clear the screen?

In case you ever intend to compile that program as C++, you can't call main() in a C++ program.

1

In case you ever intend to compile that program as C++, you can't call main() in a C++ program.

And you should never call main() in C either.

0

But standard doesn't stops anyone.

Standard also has gets() and goto , too. And when you're driving, the brake doesn't have to be used at a red light.

Bad practices are bad practices, even when they are allowed. The C standard also allows this program to compile and run:

#include <stdio.h>
int GetDenom(int val,int *den){int i;int dp=0;for(i=1;i<=(val/2);i++){if((val%i)==0){den[dp++]=i;}}den[dp++]=val;return(dp);}
int main(){int i, v;int val,den[100],denp;v=0;for(val=2;val<10000;val++){denp=GetDenom(val,den);if(v<denp)v=denp;printf("%4d",val);printf("(%3d):",denp);for(i=0;i<denp;i++)printf("%3d",den[i]);printf("\n");}printf("v=%d\n",v);return 0;}
0

Yes i completely agree with Mr. WaltP, since old habits die hard. For small exercies it doesnt make a difference, but in the long run it causes a lot of problems. Correct methodology coupled with technical accuracy can only be achieved only if you try for it and want to learn new each day.
Otherwise its the same thing like many people who learn such things the hard way. (eg. building a 3d Game using an external engine).

Hope it helped, bye.

1

Yes i completely agree with Mr. WaltP

Mr.? Makes me feel old.... :)

For small exercies it doesnt make a difference...

I really wish people would stop saying crap like this. It does make a difference whether large or small. It's just wrong. Period.

1

I really wish people would stop saying crap like this. It does make a difference whether large or small. It's just wrong. Period.

Err.. typing mistake maybe, what i wanted to say was:
"For small programming exercises it may not make a difference for you"...
and the you here is the one who thinks writing clumsy code doesnt make a difference but surely not me.

And by the way i am a proponent of methodological development and good design.

Hope it cleared away the misunderstanding.

1

Standard also has gets() and goto , too. And when you're driving, the brake doesn't have to be used at a red light.

Bad practices are bad practices, even when they are allowed. The C standard also allows this program to compile and run:

I was simply highlighting that it's legal to call in C.
main( ) is just another function in C . Nothing special about it except the return type and parameter list. Though I am not encouraging anyone to call main( ) but you just can't say that it's a bad practise just like that without giving any reason.

There are lot of things like goto , void * etc whose use is discouraged by lot of people but there can be cases where they can come handy and be used. Even Stroustrup says that goto and void* can be useful in some cases. You can't just blindly say that these are bad practises. Basic problem is that people are ignorant and really don't know where and how to use them properly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.