can anyone help me? I've made an address book. But I don't understand how to sort the information in alphabetic order. And also the information which is written in information.txt is not neat. Does anyone know how to fix it ?

here is my code..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void credit();
void menu();

void add();

void search();
void s_name();

void edit();
void e_name();
void change(FILE *editPtr, char check[],char data[]);

void delete_();
void d_name();
void empty(FILE *editPtr, char check[],char data[]);

void print();
void view();

void sort();

struct data{
       char f_name[20];
       char l_name[20];
       char address[50];
       char city [20];
       char postcode[6];
       char country[20];
       char home_phone[20];
       char work_phone[20];
       char mobile_phone[20];
       char email[30];
}record;

int main()
{
    credit();
    system("pause");
    menu();
}

void credit()
{
     printf("Simple Personal Information Manager Project\n");
     printf("===========================================\n");
     printf("\nMade by : \n");
     printf("1. Bayu Dhanubroto\n");
     printf("2. Oky Mauludany\n");
     printf("\nFinal Project\nProgramming Principles\n01PBC\n\n");
     printf("===========================================\n");
}//end credit function

void menu()
{
     int choice;
     int counter=1;
     
     while(counter!=-1){
     system("cls");
     printf("Personal Information Manager\n\n");
     printf("What do you want to do?\n");
     printf("[1]. Add Information\n");
     printf("[2]. Search Information\n");
     printf("[3]. Edit Information\n");
     printf("[4]. Delete Information\n");
     printf("[5]. View All Information\n");
     printf("[6]. Exit\n\n");
     printf("Please choose an option : ");
     scanf("%d", &choice);
     
     if(choice<1 || choice >6){
                 printf("Please only enter 1-5!\n");
                 system("pause");
     }//end if
     
     else {
     switch(choice){
     case 1 : add(); break;
     case 2 : search(); break;
     case 3 : edit(); break;
     case 4 : delete_(); break;
     case 5 : view(); break;
     case 6 : exit(1); break;
     }//end switch
     
     }//end else 
     
     }//end while
     
    
}//end menu function

void add()
{
     FILE *cfPtr;
     int choice;
     char buffer[10];
     
     system("cls");
          
     //fopen opens the file; exits if file cannot be opened
     if((cfPtr = fopen("information.txt","ab"))==NULL){
               printf("File could not be opened.\n");
               }//end if
     
     else {
          printf("======Adding New Record======\n");
          printf("\nEnter first name : ");
          gets(buffer);
          gets(record.f_name);
          record.f_name[0]=toupper(record.f_name[0]);
          printf("Enter last name : ");
          gets(record.l_name);
          record.l_name[0]=toupper(record.l_name[0]);
          printf("Enter the address : ");
          gets(record.address);
          printf("Enter the city : ");
          gets(record.city);
          record.city[0]=toupper(record.city[0]);
          printf("Enter the post code : ");
          gets(record.postcode);
          printf("Enter the country : ");
          gets(record.country);
          record.country[0]=toupper(record.country[0]);
          printf("Enter Phone Number\n");
          printf("Home : ");
          gets(record.home_phone);
          printf("Work : ");
          gets(record.work_phone);
          printf("Mobile : ");
          gets(record.mobile_phone);
          printf("Enter email address : ");
          gets(record.email);
          
          fwrite(&record, sizeof(record), 1, cfPtr);
         
          
          printf("\nRecord Saved\n");
          printf("Do you want to continue ? [1]. Yes  [2]. No\n");
          scanf("%d", &choice);
          
          if (choice == 1){
          add();
          }//end if
          fclose(cfPtr);          
          }//end else
                           
}//end add function

void search()
{
     int choice;
     int counter=1;
     
     
     system("cls");
     printf("[1]. Search by name\n");
     printf("[2]. Return to menu\n");
     printf("\nPlease choose an option : ");
     scanf("%d", &choice);
     
     if(choice<1 || choice >2){
                 printf("Please only enter 1-2!\n");
                 system("pause");
                 search();
     }//end if
     
     else {
          switch(choice){
          case 1 : s_name(); break;
          case 2 : menu(); break;
          }//end switch
          
     }//end else
    
     system("pause");

}

void s_name()
{
     FILE *cfPtr;
     int result;
     char check_f_name[20];
     char check_l_name[20];
     char buffer[10];
     int choice;
     int counter=1;
     
     
     system("cls");
     printf("[1]. Search from the first name\n");
     printf("[2]. Search from the last name\n\n");
     printf("Please choose an option : ");
     scanf("%d", &choice);
     
     if(choice<1 || choice >2){
                 printf("Please only enter 1-2!\n");
                 system("pause");
                 s_name();                
     }//end if
     
     else {
          switch(choice){
          case 1 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the first name : ");
               gets(buffer);
               gets(check_f_name);
               check_f_name[0]=toupper(check_f_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_f_name, record.f_name));
               
               result = strcmp(check_f_name, record.f_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }//end if
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
               }//end imner else
               
               fclose(cfPtr);
               system("pause");
               search();     
               }//end else     
               break;
          case 2 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the last name : ");
               gets(buffer);
               gets(check_l_name);
               check_l_name[0]=toupper(check_l_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_l_name, record.l_name));
               
               result = strcmp(check_l_name, record.l_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }//end if
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
               }//end else
               
               fclose(cfPtr);
               system("pause");
               search();     
               }//end else     
               break;
     }//end switch
     }//end else
    
     
}

void edit()
{
     int choice;
     int counter=1;     
     
     
     system("cls");
     printf("[1]. Edit by name\n");
     printf("[2]. Return to menu\n");
     printf("\nPlease choose an option : ");
     scanf("%d", &choice);
     
     if(choice<1 || choice>2){
                   printf("Please only enter 1-2!\n");
                   system("pause");
                   edit();
                   }//end inner if
     switch (choice){
            case 1 : e_name(); break;
            case 2 : menu(); break;
         
     }//end switch
    
     
}//end edit function

void e_name()
{
     FILE *cfPtr;
     int result;
     char check_f_name[20];
     char check_l_name[20];
     char buffer[10];
     int choice1;
     int choice2;
     
     system("cls");
     printf("[1]. Search from the first name\n");
     printf("[2]. Search from the last name\n\n");
     printf("Please choose an option : ");
     scanf("%d", &choice1);
     
     if(choice1<1 || choice1 >2){
                 printf("Please only enter 1-2!\n");
                 system("pause");
                 e_name();
     }//end if
     
     else {
          switch(choice1){
          case 1 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the first name : ");
               gets(buffer);
               gets(check_f_name);
               check_f_name[0]=toupper(check_f_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_f_name, record.f_name));
               
               result = strcmp(check_f_name, record.f_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
                   
                   printf("Do you want to edit this record?\n");
                   printf("[1]. Yes / [2]. No\n");
                   printf("Choose an option : ");
                   scanf("%d", &choice2);
                   
                   if(choice2<1 || choice2>2){
                   printf("Please only enter 1-2!\n");
                   system("pause");
                   e_name();
                   }//end inner if
                   
                   else {
                   switch (choice2){
                   case 1 : 
                        change(cfPtr, check_f_name, record.f_name);
                        printf("\nRecord edited!\n");
                        system("pause");
                        break;
                   
                   case 2 : 
                        menu();
                        break;
                   }//end switch 
                   }//end inner else                
               }//end else
               
               
               fclose(cfPtr);     
               }//end else     
               break;
               
          case 2 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the last name : ");
               gets(buffer);
               gets(check_l_name);
               check_l_name[0]=toupper(check_l_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_l_name, record.l_name));
               
               result = strcmp(check_l_name, record.l_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
                   
                   printf("Do you want to edit this record?\n");
                   printf("[1]. Yes / [2]. No\n");
                   printf("Choose an option : ");
                   scanf("%d", &choice2);
                   
                   if(choice2<1 || choice2>2){
                   printf("Please only enter 1-2!\n");
                   system("pause");
                   e_name();
                   }//end inner if
                   
                   else {
                   switch (choice2){
                   case 1 : 
                        change(cfPtr, check_l_name, record.l_name);
                        printf("\nRecord edited!\n");
                        system("pause");
                        break;
                   
                   case 2 : 
                        menu();
                        break;
                   }//end switch 
                   }//end inner else      
               }//end else
               
               
               fclose(cfPtr);     
               }//end else     
               break;
     }//end switch
     }//end else
     
}//end e_name function


void change(FILE *editPtr, char check[],char data[])
{
     char buffer[10];
     rewind(editPtr);
     while(fread(&record, sizeof(record), 1, editPtr) && strcmp(check,data));
     
     if(!strcmp(check,data)){
     system("cls");                        
     printf("\nEnter first name : ");
     gets(buffer);
     gets(record.f_name);
     record.f_name[0]=toupper(record.f_name[0]);
     printf("Enter last name : ");
     gets(record.l_name);
     record.l_name[0]=toupper(record.l_name[0]);
     printf("Enter the address : ");
     gets(record.address);
     printf("Enter the city : ");
     gets(record.city);
     record.city[0]=toupper(record.city[0]);
     printf("Enter the post code : ");
     gets(record.postcode);
     printf("Enter the country : ");
     gets(record.country);
     record.country[0]=toupper(record.country[0]);
     printf("Enter Phone Number\n");
     printf("Home : ");
     gets(record.home_phone);
     printf("Work : ");
     gets(record.work_phone);
     printf("Mobile : ");
     gets(record.mobile_phone);
     printf("Enter email address : ");
     gets(record.email);
     fseek(editPtr, ftell(editPtr) - sizeof(record),0);     
     fwrite(&record, sizeof(record), 1, editPtr);
     
     }//end if
     fclose(editPtr);
}

void delete_()
{
     int choice;
     
     system("cls");
     printf("[1]. Delete by name\n");
     printf("[2]. Return to menu\n");
     printf("\nPlease choose an option : ");
     scanf("%d", &choice);
     
     if(choice<1 || choice >2){
                 printf("Please only enter 1-2!\n");
                 system("pause");
                 delete_();
     }//end if
     
     else {
          switch(choice){
          case 1 : d_name(); break;
          case 2 : menu(); break;
          
          }//end switch
     }//end else
     
          
     system("pause");
}//end delete function

void d_name()
{
     FILE *cfPtr;
     int result;
     char check_f_name[20];
     char check_l_name[20];
     char buffer[10];
     int choice1;
     int choice2;
     
     system("cls");
     printf("[1]. Search from the first name\n");
     printf("[2]. Search from the last name\n\n");
     printf("Please choose an option : ");
     scanf("%d", &choice1);
     
     if(choice1<1 || choice1 >2){
                 printf("Please only enter 1-2!\n");
                 system("pause");
                 d_name();
     }//end if
     
     else {
          switch(choice1){
          case 1 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the first name : ");
               gets(buffer);
               gets(check_f_name);
               check_f_name[0]=toupper(check_f_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_f_name, record.f_name));
               
               result = strcmp(check_f_name, record.f_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
                   
                   printf("Do you want to delete this record?\n");
                   printf("[1]. Yes / [2]. No\n");
                   printf("Choose an option : ");
                   scanf("%d", &choice2);
                   
                   if(choice2<1 || choice2>2){
                   printf("Please only enter 1-2!\n");
                   system("pause");
                   d_name();
                   }//end inner if
                   
                   else {
                   switch (choice2){
                   case 1 : 
                        empty(cfPtr, check_f_name, record.f_name);
                        printf("Record deleted!\n");
                        break;
                   
                   case 2 : 
                        menu();
                        break;
                   }//end switch 
                   }//end inner else                
               }//end else
               
               
               fclose(cfPtr);     
               }//end else     
               break;
               
          case 2 : 
               if((cfPtr = fopen("information.txt","rb+"))==NULL){
               printf("File could not be opened.\n");
               }//end if
               
               else{
               printf("Enter the last name : ");
               gets(buffer);
               gets(check_l_name);
               check_l_name[0]=toupper(check_l_name[0]);
               
               while(fread(&record,sizeof(record), 1, cfPtr) 
               && strcmp(check_l_name, record.l_name));
               
               result = strcmp(check_l_name, record.l_name);
               if(result!=0)
               {
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record not found!\n\n");         
               }
               else{
                   system("cls");
                   printf("==SEARCHING==\n");
                   system("pause");
                   system("cls");
                   printf("Record found!\n\n");
                   
                   print();
                   
                   printf("Do you want to delete this record?\n");
                   printf("[1]. Yes / [2]. No\n");
                   printf("Choose an option : ");
                   scanf("%d", &choice2);
                   
                   if(choice2<1 || choice2>2){
                   printf("Please only enter 1-2!\n");
                   d_name();
                   }//end inner if
                   
                   else {
                   switch (choice2){
                   case 1 : 
                        empty(cfPtr, check_l_name, record.l_name);
                        printf("Record deleted!\n");
                        break;
                   
                   case 2 : 
                        menu();
                        break;
                   }//end switch 
                   }//end inner else      
               }//end else
               
               
               fclose(cfPtr);     
               }//end else     
               break;
     }//end switch
     }//end else
     
}//end d_name function

void empty(FILE *delPtr, char check[],char data[])
{
      int result;
      FILE *temp;
      
      temp = fopen("temp.txt","w");
      rewind(delPtr);
      
      while (fread(&record, sizeof(record), 1, delPtr))
      {
      if (strcmp(check,data)){
      fwrite(&record, sizeof(record), 1, temp);
      }//end if
      }//end while

      fclose(delPtr); 
      fclose(temp);
      remove("information.txt");
      rename("temp.txt", "information.txt");
      
      record.f_name[0]='\0';
      record.l_name[0]='\0';
      record.address[0]='\0';
      record.city[0]='\0';
      record.postcode[0]='\0';
      record.country[0]='\0';
      record.home_phone[0]='\0';
      record.work_phone[0]='\0';
      record.mobile_phone[0]='\0';
      record.email[0]='\0';           
    
}//end empty function

void print()
{
     printf("%s%s\n", "First name = ", record.f_name); 
     printf("%s%s\n", "Last name = ", record.l_name);
     printf("%s%s\n", "Address = ",record.address);
     printf("%s%s\n", "City = ", record.city);
     printf("%s%s\n", "Post code = ", record.postcode);
     printf("%s%s\n", "Country = ", record.country); 
     printf("%s%s\n", "Home phone = ", record.home_phone);
     printf("%s%s\n", "Work phone = ", record.work_phone); 
     printf("%s%s\n", "Mobile phone = ", record.mobile_phone);
     printf("%s%s\n", "Email = ", record.email);
     printf("\n");
}

void view()
{
     FILE *viewPtr;
     
     system("cls");
     if((viewPtr=fopen("information.txt","r"))== NULL){
        printf("File could not be opened.\n");
     }//end if
     
     else{
     rewind(viewPtr);
     
     while(!feof(viewPtr)){
        record.f_name[0]='\0';
        record.l_name[0]='\0';
        record.address[0]='\0';
        record.city[0]='\0';
        record.postcode[0]='\0';
        record.country[0]='\0';
        record.home_phone[0]='\0';
        record.work_phone[0]='\0';
        record.mobile_phone[0]='\0';
        record.email[0]='\0';   
        
        fread(&record, sizeof(record),1,viewPtr);
        if(record.f_name[0]!='\0'){
        print();
        }
        else {
        break;
        }
        
        }//end while
        fclose(viewPtr);
     }//end else
     system("pause");
}

void sort()
{
      FILE *temp;
      
}

I'm sorry my english is really2 bad..
I mean, if you check in information.txt ..
all the data is stored always to the right. What I want is, if I input the data, the first data is on the top. Then, the next data is stored on the next line (not on the same line).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.