Link to home
Start Free TrialLog in
Avatar of yunikon
yunikon

asked on

use of clrscr()

I wrote a code in C that sorts files according to their names, size, date etc.  The results are displayed on the output console. Inputs to the console are also requested from the user via a menu display.  I've used clrscr() at various points in the code to get a better organized display.  Some random errors were showing up in some of my sort result displays.  Zeros were appearing in date and time values where there were no zeros previously.  At times one file may seem to have not been sorted.   After verifying that relevant functions were correct I noticed that removing and relocating some clrscr() solved the problem.   I learned on the web that clrscr clears the screen prior to writing to the terminal, it is a command and so must appear after variable declarations and it sets all character positions to blanks with the currently defined text.  I'm having the display errors again in another program and would like any help on the correct use of the clrscr().
Using Borland C++ 5.02 application.

-y

 
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yunikon
yunikon

ASKER

What's tuning my printf's?
It's a long program.  Trying to remember how to  post code so that its format is not distorted but I can't remember that now.  Intended to post question in C programming section.  Discovered that I did not when searching for the post.  Guess I was a bit sleepy when I made the error.
 

/*               NOTES
               -----

1. If reading files from the A drive ENSURE THAT THE 3.5 FLOPPY IS INSERTED. */




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dir.h>
#include <string.h>
#include <dos.h>
#include <io.h>
#include <time.h>





            // file attribute definitions

#define      NORMAL            0x00
#define      READONLY            0x01
#define      HIDDEN            0x02
#define      SYSTEM            0x04
#define      VOLLABEL            0x08
#define      DIR                  0x10
#define      ARCHIVE            0x20


typedef struct fileTime {
                           int id;

                                                      int sec;

                           int minute;

                           int hour;

                           char Am_Pm[3];

                        }F_TIME;



typedef struct fileDate {
                                                      int day;

                           int month;

                           int year;

                        }F_DATE;



typedef struct fileblock {
                                                 char ff_reserved[22];

                         char ff_attrib;

                         unsigned short ff_ftime;

                         unsigned short ff_fdate;

                         long ff_fsize;

                         char ff_name[13];

                         F_TIME theTime;

                         F_DATE theDate;

                         struct fileblock *next;

                      }FBLK;


typedef FBLK *FBLKPTR;


int menu();
void find_Files(FBLKPTR *, char []);
void nameSort(FBLK *);
void sizeSort(FBLK *);
void dateSort(FBLK *);
int listLength(FBLK *);
int comp_Dates(short int, short int);
char listOrder();
void delete_File(FBLK *);
void rename_File(FBLK *);
void add_File(FBLK *);
int findFile(FBLK *, char []);
FBLKPTR convert_from_DOS_date(FBLK *);
FBLKPTR convert_from_DOS_time(FBLK *);
F_TIME ConvertClock_to_AmPm(F_TIME);
FBLKPTR convert_to_DOS_date(FBLKPTR *);
FBLKPTR convert_to_DOS_time(FBLKPTR *);
void printfiles(FBLK *);


main()
{

      char path[10];
      FBLK *head, *newFile, *node;
      int choice;

      newFile = new FBLK;

      if(newFile == NULL) {

            puts("\nERROR, NO MEMORY !!\n\n");

         getch();

         return 1;

      }

      node = new FBLK;

          if(node == NULL) {

             puts("\nERROR, NO MEMORY\n");

          getch();

          return 1;

      }

      head = node;

      do
            {

      clrscr();

            puts("\n\n\t\t\tFOLDER MANAGER MENU");
      puts("\t\t\t---------------------\n");

      puts("\t\t\t1. ACCESS FILE LIST.\n");

      puts("\t\t\t2. LIST FILES BY NAME ORDER\n");

      puts("\t\t\t3. LIST FILES BY SIZE ORDER\n");

      puts("\t\t\t4. LIST FILES BY DATE ORDER\n");

      puts("\t\t\t5. DELETE A FILE FROM THE LIST\n");

      puts("\t\t\t6. RENAME A FILE ALREADY IN THE LIST\n");

      puts("\t\t\t7. ADD NEW FILES TO THE LIST\n");

      puts("\t\t\t8. EXIT\n");

      puts("\n\tSelect a Menu Item Number ?\n");

      scanf("%d", &choice);

      clrscr();

      switch(choice) {

              case 1:

              puts("\n\nENTER NAME AND PATH OF FOLDER  -> [a for A drive] ?\n\n");

              scanf("%s", &path);

              find_Files(&head, path);

              printfiles(head);

              break;

            case 2:

              clrscr();

              nameSort(head);

              printfiles(head);

              clrscr();

              break;

            case 3:

              sizeSort(head);

              printfiles(head);

              clrscr();

              break;

     case 4:

              clrscr();


              dateSort(head);

              printfiles(head);

              break;


      case 5:

              clrscr();

              delete_File(head);

              printfiles(head);

              clrscr();

              break;

      case 6:

                    clrscr();

              rename_File(head);

              printfiles(head);

              clrscr();

              break;


      case 7:
              clrscr();

              add_File(head);

              clrscr();

              printfiles(head);

              clrscr();

              break;


      case 8:

                    puts("\n\nExiting Folder Manager.......\n\n");  sleep(2);

                 exit(0);

              break;

      default:

              puts("\n\n\t\tINVALID MENU CHOICE !!");

              break;

      }

    }


     while(choice != 8);

      puts("\n\nExiting Folder Manager......sleep(2)\n\n");

      puts("\n\nEND\n\n");

      sleep(2);

      return 0;

}



void find_Files(FBLKPTR *head, char directory[])
{


            int done;

      struct ffblk fblock;

      FBLK *node, *d, *t;


      node = new FBLK;

         if(node == NULL)  puts("\nERROR, NO MEMORY\n");

      *head = node;

         printf("\n\tDirectory listing of %s\n\n", directory);

      done = findfirst("a:\*.*", &fblock, NORMAL);

        while(!done) {

      strcpy((*node).ff_name, fblock.ff_name);

      (*node).ff_fsize = fblock.ff_fsize;

      (*node).ff_fdate = fblock.ff_fdate;

      (*node).ff_ftime = fblock.ff_ftime;


      d = convert_from_DOS_date(node);   //  convert 16-bit date to day-month-year

      t = convert_from_DOS_time(node);   //   convert 16-bit time to 24-hour clock


      printf("\n%s\t%ld\t\t%d/%d/%d\t%d:%d:%d\n", (*node).ff_name,

                                                                         (*node).ff_fsize,

                                        d->theDate.day,

                                        d->theDate.month,

                                        d->theDate.year,

                                        t->theTime.hour,

                                        t->theTime.minute,

                                        t->theTime.sec);


      done = findnext(&fblock);

      if(done == - 1)    // exit loop and prevent creation of following node if end of file

              break;

      else {


      node->next = new FBLK;   // create new node if new file found

      if(node->next == NULL) {

            puts("\nERROR, NO MEMORY !!\n");

            getch();

            return ;

      }

      node = node->next;

     }
   }

   node->next = NULL;

   printf("\n\tBuilding dynamic list ...."); sleep(1); printf("done!\n\nFile list:\n\n");

}


char listOrder()
{

            char arrange;


    do
      {
      puts("\n\n\t\tENTER a for ascending d descending ?\n\n");
      scanf("%c", &arrange);

      clrscr();

      if(arrange == 'a' || arrange == 'A')  break;

      if(arrange == 'd' || arrange == 'D')  break;

      }while(1);


           return arrange;

}


void nameSort(FBLK *List)
{

   FBLK *prev, *curr, *tempptr, *NewNode;

   char order;

        int pass;

   tempptr       = List;
   prev             = tempptr;
   curr             = tempptr->next;


      NewNode = (FBLK *)malloc(sizeof(FBLK));

   if(NewNode == NULL) {

           puts("\nERROR, NO MEMORY\n");

        getch();

        return;

   }

   int Length = listLength(List);

   order = listOrder();

   for (pass = 1; pass <= Length; pass++)
   {


         // Loop to compare one fileName at a time with fileNames in other Nodes
         while(1)
         {
            int name_cmp = strcmp(prev->ff_name, curr->ff_name);

               if((order == 'a' && name_cmp > 0) || (order == 'd' && name_cmp < 0))
               {

                          // Store previous information in the NewNode temporarily
                          strcpy(NewNode->ff_name, prev->ff_name);

                       NewNode->ff_fsize = prev->ff_fsize;
            NewNode->ff_fdate      = prev->ff_fdate;
                       NewNode->ff_ftime      = prev->ff_ftime;


                          // Store the next node data in the previous Node

                          strcpy(prev->ff_name, curr->ff_name);
            prev->ff_fsize = curr->ff_fsize;
            prev->ff_fdate      = curr->ff_fdate;
                       prev->ff_ftime      = curr->ff_ftime;

                          // Store saved data from the NewNode to the next Node


                          strcpy(curr->ff_name, NewNode->ff_name);
            curr->ff_fsize = NewNode->ff_fsize;
                       curr->ff_fdate      = NewNode->ff_fdate;
                       curr->ff_ftime      = NewNode->ff_ftime;
                 }
               prev = curr;
               curr = curr->next;

         if(curr == NULL)
         {
                        prev = tempptr;
                     curr = tempptr->next;
            break;
         }
      }
   }


               List = prev;

         while(List != NULL) {

                List = List->next;

         }

}


void sizeSort(FBLK *head)
{

            FBLK *prev, *curr, *temp, *newptr;

      char order;

      newptr = new FBLK;

      if(newptr == NULL) {

             puts("\n\nERROR, NO MEMORY !!\n\n");

          getch();

          return;
      }

      temp = head;

      prev = temp;

      curr = temp->next;



      int Length = listLength(head);

      order = listOrder();


      for(int test = 1;  test <= Length; test++) {


      while(1) {      // compare filesizes

             int size_comp = prev->ff_fsize - curr->ff_fsize;

          if(size_comp > 0 && order == 'a' || size_comp < 0 && order == 'd') {

               strcpy(newptr->ff_name, prev->ff_name);
               newptr->ff_fsize = prev->ff_fsize;
               newptr->ff_fdate = prev->ff_fdate;
               newptr->ff_ftime = newptr->ff_ftime;

               strcpy(prev->ff_name, curr->ff_name);
               prev->ff_fsize = curr->ff_fsize;
               prev->ff_fdate = curr->ff_fdate;
               prev->ff_ftime = curr->ff_ftime;

               strcpy(curr->ff_name, newptr->ff_name);
               curr->ff_fsize = newptr->ff_fsize;
               curr->ff_fdate = newptr->ff_fdate;
               curr->ff_ftime = newptr->ff_ftime;

          }

          prev = curr;
          curr = curr->next;    // step to next file in link list

          if(curr == NULL) {

                      prev = temp;
               curr = temp->next;
               break;
          }

      }

   }

}


void dateSort(FBLK *head)
{
 //     clrscr();

            FBLK *prev, *curr, *temp, *newptr;

      int pass, date_comp;

      char order;

      newptr = new FBLK;

      if(newptr == NULL) {

                  puts("\nERROR, NO MEMORY !!\n");

            getch();

            return;
      }

      temp = head;

      prev = temp;

      curr = temp->next;

      int Length = listLength(head);

      order = listOrder();


   for(pass = 1; pass <= Length; pass++) {


       while(1) {


              date_comp = comp_Dates(prev->ff_fdate, curr->ff_fdate);

          if((order == 'a' && date_comp == 1 ) || (order == 'd' && date_comp == 2)) {

              strcpy(newptr->ff_name, prev->ff_name);

              newptr->ff_fsize = prev->ff_fsize;

              newptr->ff_fdate = prev->ff_fdate;

              newptr->ff_ftime = prev->ff_ftime;


              strcpy(prev->ff_name, curr->ff_name);

              prev->ff_fsize = curr->ff_fsize;

              prev->ff_fdate = curr->ff_fdate;

              prev->ff_ftime = curr->ff_ftime;


              strcpy(curr->ff_name, newptr->ff_name);

              curr->ff_fsize = newptr->ff_fsize;

              curr->ff_fdate = newptr->ff_fdate;

              curr->ff_ftime = newptr->ff_ftime;

             }

                prev = curr;
                curr = curr->next;

                if(curr == NULL) {
                         prev = temp;
                         curr = temp->next;
                         break;
                }


       }

    }

}




int listLength(FBLK *top)
{

            FBLK *temp;

      int count = 0;

      temp = top;

      while(temp != NULL) {

            temp = temp->next;

            count++;
      }

      count++;

      return count;

}


int comp_Dates(short int date1, short int date2)
{

        if(date1 == date2) return 0;

              return (date1 > date2) ? 1 : 2;

}


void delete_File(FBLK *dhptr)
{


      FBLK *prev, *currptr, *temp;

      char to_remove[13], deleting;

      puts("\n\nENTER NAME OF FILE TO BE DELETED ?\n\n");

      scanf("%s", to_remove);

      currptr = dhptr;

      if(dhptr == NULL) puts("\nLIST IS EMPTY !!\n\n");


      while(currptr != NULL && strcmp(currptr->ff_name, to_remove) != 0) {

               prev = currptr;

            currptr = currptr->next;
      }

      if(currptr == NULL) {

            printf("\n\nfilename  %s does not exist !!\n\n", to_remove);

         getch();

         return;

      }

      printf("\n\ndeleting file %s....      continue ?  y/n\n\n", currptr->ff_name);

      scanf("%c",&deleting);

      if(deleting == 'n')

                  return;

      else {

      temp = currptr;

      prev->next = currptr->next;

      free (temp);

      }

}


void rename_File(FBLK *dhptr)
{

      char presName[13], newName[13];

      FBLK *curr;

      curr = dhptr;

      puts("\n\n\t\tENTER PRESENT NAME OF FILE ?\n\n");

      scanf("%s", presName);

     puts("\n\n\t\tENTER NEW NAME FOR FILE ?\n\n");

     scanf("%s", newName);


     while(curr != NULL && strcmp(curr->ff_name, presName) != 0) {

         curr = curr->next;

     }

     if(curr != NULL)

                 strcpy(curr->ff_name, newName);

     else

             printf("\n\nFILENAME  %s IS NOT IN THE LIST !!\n\n", presName);


}


void add_File(FBLK *dhptr)
{

            FBLK *newFile, *prev, *curr;

      char addname[20];


      newFile = new FBLK;

      if(newFile == NULL) {

            puts("\nERROR, NO MEMORY\n");

         getch();

         return;

      }

      puts("\n\n\t\tENTER NAME OF FILE TO BE ADDED ?\n\n");

      scanf("%s", addname);

      strcpy(newFile->ff_name, addname);

      int exist = findFile(dhptr, addname);    // test to prevent filename duplication

      if(exist == 1) {

         puts("\nFILE EXISTS ALREADY !!\n");

         getch();

         return;

      }

      puts("\n\n\t\tENTER SIZE OF FILE IN BYTES ?\n\n");

      scanf("%ld", &newFile->ff_fsize);

      puts("\n\n\t\tENTER TODAY'S DATE  (dd mm yyyy) ?\n\n");

      scanf("%d%d%d", &newFile->theDate.day, &newFile->theDate.month, &newFile->theDate.year);

      puts("\n\n\t\tENTER THE TIME (hh mm ss) ?\n\n");

      scanf("%d%d%d", &newFile->theTime.hour, &newFile->theTime.minute, &newFile->theTime.sec);

      puts("\n\nENTER   'am' or 'pm' ?\n\n");

      scanf("%s", newFile->theTime.Am_Pm);


      getch();

      newFile = convert_to_DOS_date(&newFile);    // convert entered file date and time
                                                  // from conventional to DOS formats
      newFile = convert_to_DOS_time(&newFile);


      curr = dhptr;        // adding new file to the link list

      while(curr != NULL) {

              prev = curr;

           curr = curr->next;
      }

   if(prev == NULL) {            //  if list is empty

      newFile->next = prev;

      prev = newFile;

  }

  newFile->next = curr;

  prev->next = newFile;

}


                        // test for file to prevent duplication - add_File()

int findFile(FBLK *head, char newName[])
{

      FBLK *curr;


      curr = head;

            while(curr != NULL && strcmp(curr->ff_name, newName) != 0) {


         curr = curr->next;

     }

     if(curr != NULL)

                 return 1;

     return 0;

}

                // convert file date from DOS to conventional format for display

FBLKPTR convert_from_DOS_date(FBLK *fnode)
{

       FBLK *newfile;

       newfile = new FBLK;

       if(newfile == NULL) {

                   puts("\nERROR, NO MEMORY\n");

            getch();

       }


       unsigned short dmask = 31;

       unsigned short mmask = 480;

       unsigned short ymask = 65024;


       newfile->theDate.day = fnode->ff_fdate & dmask;

       newfile->theDate.month = fnode->ff_fdate & mmask;;

       newfile->theDate.year = fnode->ff_fdate & ymask;


       newfile->theDate.month =  newfile->theDate.month >> 5;
       newfile->theDate.year =  newfile->theDate.year >> 9;

       newfile->theDate.year += 1980;     // base year is 1980, DOS


       return newfile;


}


      // convert file dates from conventional to DOS format for comparison -  dateSort()

FBLKPTR convert_to_DOS_date(FBLKPTR *newfileDate)
{

            FBLK *dos_file;


      int *conv[3], SUM;


      dos_file =  *newfileDate;

      dos_file->theDate.year -= 1980;    // DOS, base year = 1980

      dos_file->theDate.year *= 512;

      dos_file->theDate.month *= 32;

      conv[0] = &dos_file->theDate.day;

      conv[1] = &dos_file->theDate.month;

      conv[2] = &dos_file->theDate.year;

      SUM = *conv[0] + *conv[1] + *conv[2];

      dos_file->ff_fdate = (unsigned short)SUM;    // typecast SUM from int to unsigned short

      return dos_file;

}

           // convert file time from conventional to DOS format, add_File()

FBLKPTR convert_to_DOS_time(FBLKPTR *newfileTime)
{


            FBLK *dos_file;


      int *conv[3], SUM;


      dos_file =  *newfileTime;

      dos_file->theTime.hour *= 2048;

      dos_file->theTime.minute *= 32;

      dos_file->theTime.sec /= 2;

      conv[0] = &dos_file->theTime.hour;

      conv[1] = &dos_file->theTime.minute;

      conv[2] = &dos_file->theTime.sec;

      SUM = *conv[0] + *conv[1] + *conv[2];

      dos_file->ff_ftime = (unsigned short)SUM;      // typecast SUM from int to unsigned short


      return dos_file;

}

                // convert file time from DOS to conventional format for display

FBLKPTR convert_from_DOS_time(FBLK *f_Time)
{


            FBLK *nodeTIME;
      F_TIME ft;

      nodeTIME = new FBLK;

      if(nodeTIME == NULL) {

                  puts("\nERROR, NO MEMORY!!\n");
             getch();

             exit(1);

      }

      unsigned short smask = 31;

      unsigned short mmask = 2016;

      unsigned short hmask = 63488;

      nodeTIME->theTime.sec = f_Time->ff_ftime & smask;      // ffblock.ff_ftime was received from main at function
         nodeTIME->theTime.minute = f_Time->ff_ftime &mmask;      // call, it is stored in int f_Time, so f_Time is used
         nodeTIME->theTime.hour  = f_Time->ff_ftime & hmask;      // here instead of fblock.ff_ftime.

         nodeTIME->theTime.minute = nodeTIME->theTime.minute >> 5;
         nodeTIME->theTime.hour  = nodeTIME->theTime.hour >> 11;

             ft = ConvertClock_to_AmPm(nodeTIME->theTime);

      nodeTIME->theTime = ft;


         return nodeTIME;

}



F_TIME ConvertClock_to_AmPm(F_TIME fileTime)
{

      F_TIME newTime;


      if(fileTime.sec >= 60) {

                  fileTime.minute += fileTime.sec / 60;

               fileTime.sec  = fileTime.sec % 60;

      }

      newTime.sec = fileTime.sec;

      if(fileTime.minute >= 60) {

            fileTime.hour += fileTime.minute / 60;

            fileTime.minute = fileTime.minute % 60;

      }

      newTime.minute = fileTime.minute;


      if(fileTime.hour <= 12) {

           newTime.hour = fileTime.hour;

           strcpy(newTime.Am_Pm, "AM");

       }

       else {


           newTime.hour = fileTime.hour;

           strcpy(newTime.Am_Pm, "PM");

       }

      return newTime;

}


void printfiles(FBLK *dhptr)
{

            FBLK *newfile, *d, *t;

            newfile = dhptr;


            if(newfile == NULL) {


               puts("\nLIST IS EMPTY !!\n");

            getch();

            return;

         }


         puts("\n\nFILE  NAME      FILE-SIZE         FILE-DATE          FILE TIME");
            puts("------------------------------------------------------------\n");
         while(newfile != NULL) {

              d  = convert_from_DOS_date(newfile);

              t  = convert_from_DOS_time(newfile);

                   printf("\n%s\t %d\t %d:%d:%d\t%d:%d:%d%s\n", newfile->ff_name,
                                                    newfile->ff_fsize,

                              d->theDate.day,
                              d->theDate.month,
                              d->theDate.year,

                              t->theTime.hour,
                              t->theTime.minute,
                              t->theTime.sec,
                              t->theTime.Am_Pm);


                     newfile = newfile->next;

                    }

              puts("\n\n\n\n\n\t\t\tPRESS ENTER TO CONTINUE !!\n\n");

              getch();

         return;

}
Avatar of yunikon

ASKER

I've made the following two corrections. Function listOrder, flush input buffer. Function sizeSort the second
newptr->ff_ftime was changed to prev->ff_ftime.  All clrscr() were removed. When a new file is added (menu 7) it isn't sorted properly by nameSort() unless the filename is the last or highest.  eg if filenames algebra, chemistry and history exist and you add a file named zoology the files are sorted by nameSort() correctly.  If a file named english is added, nameSort() doesn't sort the files properly.


/*               NOTES
               -----

1. If reading files from the A drive ENSURE THAT THE 3.5 FLOPPY IS INSERTED. */




#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dir.h>
#include <string.h>
#include <dos.h>
#include <io.h>
#include <time.h>





          // file attribute definitions

#define     NORMAL          0x00
#define     READONLY          0x01
#define     HIDDEN          0x02
#define     SYSTEM          0x04
#define     VOLLABEL          0x08
#define     DIR               0x10
#define     ARCHIVE          0x20


typedef struct fileTime {
                           int id;

                                             int sec;

                           int minute;

                           int hour;

                           char Am_Pm[3];

                        }F_TIME;



typedef struct fileDate {
                                             int day;

                           int month;

                           int year;

                        }F_DATE;



typedef struct fileblock {
                                         char ff_reserved[22];

                         char ff_attrib;

                         unsigned short ff_ftime;

                         unsigned short ff_fdate;

                         long ff_fsize;

                         char ff_name[13];

                         F_TIME theTime;

                         F_DATE theDate;

                         struct fileblock *next;

                      }FBLK;


typedef FBLK *FBLKPTR;


int menu();
void find_Files(FBLKPTR *, char []);
void nameSort(FBLK *);
void sizeSort(FBLK *);
void dateSort(FBLK *);
int listLength(FBLK *);
int comp_Dates(short int, short int);
char listOrder();
void delete_File(FBLK *);
void rename_File(FBLK *);
void add_File(FBLK *);
int findFile(FBLK *, char []);
FBLKPTR convert_from_DOS_date(FBLK *);
FBLKPTR convert_from_DOS_time(FBLK *);
F_TIME ConvertClock_to_AmPm(F_TIME);
FBLKPTR convert_to_DOS_date(FBLKPTR *);
FBLKPTR convert_to_DOS_time(FBLKPTR *);
void printfiles(FBLK *);


main()
{

      char path[10];
      FBLK *head, *newFile, *node;
      int choice;

      newFile = new FBLK;

      if(newFile == NULL) {

           puts("\nERROR, NO MEMORY !!\n\n");

         getch();

         return 1;

      }

      node = new FBLK;

         if(node == NULL) {

            puts("\nERROR, NO MEMORY\n");

          getch();

          return 1;

      }

      head = node;

      do
           {

      clrscr();

          puts("\n\n\t\t\tFOLDER MANAGER MENU");
      puts("\t\t\t---------------------\n");

      puts("\t\t\t1. ACCESS FILE LIST.\n");

      puts("\t\t\t2. LIST FILES BY NAME ORDER\n");

      puts("\t\t\t3. LIST FILES BY SIZE ORDER\n");

      puts("\t\t\t4. LIST FILES BY DATE ORDER\n");

      puts("\t\t\t5. DELETE A FILE FROM THE LIST\n");

      puts("\t\t\t6. RENAME A FILE ALREADY IN THE LIST\n");

      puts("\t\t\t7. ADD NEW FILES TO THE LIST\n");

      puts("\t\t\t8. EXIT\n");

      puts("\n\tSelect a Menu Item Number ?\n");

      scanf("%d", &choice);

      clrscr();

      switch(choice) {

            case 1:

              puts("\n\nENTER NAME AND PATH OF FOLDER  -> [a for A drive] ?\n\n");

              scanf("%s", &path);

              find_Files(&head, path);

              printfiles(head);

              break;

          case 2:

              clrscr();

              nameSort(head);

              printfiles(head);

              clrscr();

              break;

          case 3:

              sizeSort(head);

              printfiles(head);

              clrscr();

              break;

     case 4:

              clrscr();


              dateSort(head);

              printfiles(head);

              break;


      case 5:

              clrscr();

              delete_File(head);

              printfiles(head);

              clrscr();

              break;

      case 6:

                  clrscr();

              rename_File(head);

              printfiles(head);

              clrscr();

              break;


      case 7:
              clrscr();

              add_File(head);

              clrscr();

              printfiles(head);

              clrscr();

              break;


      case 8:

                  puts("\n\nExiting Folder Manager.......\n\n");  sleep(2);

                exit(0);

              break;

      default:

              puts("\n\n\t\tINVALID MENU CHOICE !!");

              break;

      }

    }


     while(choice != 8);

      puts("\n\nExiting Folder Manager......sleep(2)\n\n");

      puts("\n\nEND\n\n");

      sleep(2);

      return 0;

}



void find_Files(FBLKPTR *head, char directory[])
{


          int done;

      struct ffblk fblock;

      FBLK *node, *d, *t;


      node = new FBLK;

        if(node == NULL)  puts("\nERROR, NO MEMORY\n");

      *head = node;

        printf("\n\tDirectory listing of %s\n\n", directory);

      done = findfirst("a:\*.*", &fblock, NORMAL);

       while(!done) {

      strcpy((*node).ff_name, fblock.ff_name);

      (*node).ff_fsize = fblock.ff_fsize;

      (*node).ff_fdate = fblock.ff_fdate;

      (*node).ff_ftime = fblock.ff_ftime;


      d = convert_from_DOS_date(node);   //  convert 16-bit date to day-month-year

      t = convert_from_DOS_time(node);   //   convert 16-bit time to 24-hour clock


      printf("\n%s\t%ld\t\t%d/%d/%d\t%d:%d:%d\n", (*node).ff_name,

                                                              (*node).ff_fsize,

                                        d->theDate.day,

                                        d->theDate.month,

                                        d->theDate.year,

                                        t->theTime.hour,

                                        t->theTime.minute,

                                        t->theTime.sec);


      done = findnext(&fblock);

      if(done == - 1)    // exit loop and prevent creation of following node if end of file

             break;

      else {


      node->next = new FBLK;   // create new node if new file found

      if(node->next == NULL) {

           puts("\nERROR, NO MEMORY !!\n");

           getch();

           return ;

      }

      node = node->next;

     }
   }

   node->next = NULL;

   printf("\n\tBuilding dynamic list ...."); sleep(1); printf("done!\n\nFile list:\n\n");

}


char listOrder()
{

          char arrange;


    do
      {

 CORRECTN->    fflush(stdin);

      puts("\n\n\t\tENTER a for ascending d descending ?\n\n");
      scanf("%c", &arrange);

      clrscr();

      if(arrange == 'a' || arrange == 'A')  break;

      if(arrange == 'd' || arrange == 'D')  break;

CORRECTN->  fflush(stdin);

      }while(1);


          return arrange;

}


void nameSort(FBLK *List)
{

   FBLK *prev, *curr, *tempptr, *NewNode;

   char order;

       int pass;

   tempptr      = List;
   prev           = tempptr;
   curr           = tempptr->next;


     NewNode = (FBLK *)malloc(sizeof(FBLK));

   if(NewNode == NULL) {

          puts("\nERROR, NO MEMORY\n");

        getch();

        return;

   }

   int Length = listLength(List);

   order = listOrder();

   for (pass = 1; pass <= Length; pass++)
   {


        // Loop to compare one fileName at a time with fileNames in other Nodes
        while(1)
        {
            int name_cmp = strcmp(prev->ff_name, curr->ff_name);

             if((order == 'a' && name_cmp > 0) || (order == 'd' && name_cmp < 0))
             {

                      // Store previous information in the NewNode temporarily
                      strcpy(NewNode->ff_name, prev->ff_name);

                    NewNode->ff_fsize = prev->ff_fsize;
            NewNode->ff_fdate     = prev->ff_fdate;
                    NewNode->ff_ftime     = prev->ff_ftime;


                      // Store the next node data in the previous Node

                      strcpy(prev->ff_name, curr->ff_name);
            prev->ff_fsize = curr->ff_fsize;
            prev->ff_fdate     = curr->ff_fdate;
                    prev->ff_ftime     = curr->ff_ftime;

                      // Store saved data from the NewNode to the next Node


                      strcpy(curr->ff_name, NewNode->ff_name);
            curr->ff_fsize = NewNode->ff_fsize;
                    curr->ff_fdate     = NewNode->ff_fdate;
                    curr->ff_ftime     = NewNode->ff_ftime;
               }
             prev = curr;
             curr = curr->next;

         if(curr == NULL)
         {
                    prev = tempptr;
                  curr = tempptr->next;
            break;
         }
      }
   }


             List = prev;

         while(List != NULL) {

               List = List->next;

         }

}


void sizeSort(FBLK *head)
{

          FBLK *prev, *curr, *temp, *newptr;

      char order;

      newptr = new FBLK;

      if(newptr == NULL) {

            puts("\n\nERROR, NO MEMORY !!\n\n");

          getch();

          return;
      }

      temp = head;

      prev = temp;

      curr = temp->next;



      int Length = listLength(head);

      order = listOrder();


      for(int test = 1;  test <= Length; test++) {


      while(1) {      // compare filesizes

            int size_comp = prev->ff_fsize - curr->ff_fsize;

          if(size_comp > 0 && order == 'a' || size_comp < 0 && order == 'd') {

               strcpy(newptr->ff_name, prev->ff_name);
               newptr->ff_fsize = prev->ff_fsize;
               newptr->ff_fdate = prev->ff_fdate;
  ERROR->     newptr->ff_ftime = newptr->ff_ftime;

               strcpy(prev->ff_name, curr->ff_name);
               prev->ff_fsize = curr->ff_fsize;
               prev->ff_fdate = curr->ff_fdate;
               prev->ff_ftime = curr->ff_ftime;

               strcpy(curr->ff_name, newptr->ff_name);
               curr->ff_fsize = newptr->ff_fsize;
               curr->ff_fdate = newptr->ff_fdate;
               curr->ff_ftime = newptr->ff_ftime;

          }

          prev = curr;
          curr = curr->next;    // step to next file in link list

          if(curr == NULL) {

                    prev = temp;
               curr = temp->next;
               break;
          }

      }

   }

}


void dateSort(FBLK *head)
{
 //     clrscr();

          FBLK *prev, *curr, *temp, *newptr;

      int pass, date_comp;

      char order;

      newptr = new FBLK;

      if(newptr == NULL) {

                puts("\nERROR, NO MEMORY !!\n");

            getch();

            return;
      }

      temp = head;

      prev = temp;

      curr = temp->next;

      int Length = listLength(head);

      order = listOrder();


   for(pass = 1; pass <= Length; pass++) {


       while(1) {


             date_comp = comp_Dates(prev->ff_fdate, curr->ff_fdate);

          if((order == 'a' && date_comp == 1 ) || (order == 'd' && date_comp == 2)) {

              strcpy(newptr->ff_name, prev->ff_name);

              newptr->ff_fsize = prev->ff_fsize;

              newptr->ff_fdate = prev->ff_fdate;

              newptr->ff_ftime = prev->ff_ftime;


              strcpy(prev->ff_name, curr->ff_name);

              prev->ff_fsize = curr->ff_fsize;

              prev->ff_fdate = curr->ff_fdate;

              prev->ff_ftime = curr->ff_ftime;


              strcpy(curr->ff_name, newptr->ff_name);

              curr->ff_fsize = newptr->ff_fsize;

              curr->ff_fdate = newptr->ff_fdate;

              curr->ff_ftime = newptr->ff_ftime;

             }

               prev = curr;
               curr = curr->next;

               if(curr == NULL) {
                       prev = temp;
                       curr = temp->next;
                       break;
               }


       }

    }

}




int listLength(FBLK *top)
{

          FBLK *temp;

      int count = 0;

      temp = top;

      while(temp != NULL) {

            temp = temp->next;

            count++;
      }

      count++;

      return count;

}


int comp_Dates(short int date1, short int date2)
{

        if(date1 == date2) return 0;

            return (date1 > date2) ? 1 : 2;

}


void delete_File(FBLK *dhptr)
{


      FBLK *prev, *currptr, *temp;

      char to_remove[13], deleting;

      puts("\n\nENTER NAME OF FILE TO BE DELETED ?\n\n");

      scanf("%s", to_remove);

      currptr = dhptr;

      if(dhptr == NULL) puts("\nLIST IS EMPTY !!\n\n");


      while(currptr != NULL && strcmp(currptr->ff_name, to_remove) != 0) {

              prev = currptr;

            currptr = currptr->next;
      }

      if(currptr == NULL) {

           printf("\n\nfilename  %s does not exist !!\n\n", to_remove);

         getch();

         return;

      }

      printf("\n\ndeleting file %s....     continue ?  y/n\n\n", currptr->ff_name);

      scanf("%c",&deleting);

      if(deleting == 'n')

                return;

      else {

      temp = currptr;

      prev->next = currptr->next;

      free (temp);

      }

}


void rename_File(FBLK *dhptr)
{

      char presName[13], newName[13];

      FBLK *curr;

      curr = dhptr;

      puts("\n\n\t\tENTER PRESENT NAME OF FILE ?\n\n");

      scanf("%s", presName);

     puts("\n\n\t\tENTER NEW NAME FOR FILE ?\n\n");

     scanf("%s", newName);


     while(curr != NULL && strcmp(curr->ff_name, presName) != 0) {

         curr = curr->next;

     }

     if(curr != NULL)

               strcpy(curr->ff_name, newName);

     else

            printf("\n\nFILENAME  %s IS NOT IN THE LIST !!\n\n", presName);


}


void add_File(FBLK *dhptr)
{

          FBLK *newFile, *prev, *curr;

      char addname[20];


      newFile = new FBLK;

      if(newFile == NULL) {

           puts("\nERROR, NO MEMORY\n");

         getch();

         return;

      }

      puts("\n\n\t\tENTER NAME OF FILE TO BE ADDED ?\n\n");

      scanf("%s", addname);

      strcpy(newFile->ff_name, addname);

      int exist = findFile(dhptr, addname);    // test to prevent filename duplication

      if(exist == 1) {

         puts("\nFILE EXISTS ALREADY !!\n");

         getch();

         return;

      }

      puts("\n\n\t\tENTER SIZE OF FILE IN BYTES ?\n\n");

      scanf("%ld", &newFile->ff_fsize);

      puts("\n\n\t\tENTER TODAY'S DATE  (dd mm yyyy) ?\n\n");

      scanf("%d%d%d", &newFile->theDate.day, &newFile->theDate.month, &newFile->theDate.year);

      puts("\n\n\t\tENTER THE TIME (hh mm ss) ?\n\n");

      scanf("%d%d%d", &newFile->theTime.hour, &newFile->theTime.minute, &newFile->theTime.sec);

      puts("\n\nENTER   'am' or 'pm' ?\n\n");

      scanf("%s", newFile->theTime.Am_Pm);


      getch();

      newFile = convert_to_DOS_date(&newFile);    // convert entered file date and time
                                                  // from conventional to DOS formats
      newFile = convert_to_DOS_time(&newFile);


      curr = dhptr;        // adding new file to the link list

      while(curr != NULL) {

             prev = curr;

           curr = curr->next;
      }

   if(prev == NULL) {          //  if list is empty

      newFile->next = prev;

      prev = newFile;

  }

  newFile->next = curr;

  prev->next = newFile;

}


                       // test for file to prevent duplication - add_File()

int findFile(FBLK *head, char newName[])
{

      FBLK *curr;


      curr = head;

          while(curr != NULL && strcmp(curr->ff_name, newName) != 0) {


         curr = curr->next;

     }

     if(curr != NULL)

               return 1;

     return 0;

}

                // convert file date from DOS to conventional format for display

FBLKPTR convert_from_DOS_date(FBLK *fnode)
{

       FBLK *newfile;

       newfile = new FBLK;

       if(newfile == NULL) {

                 puts("\nERROR, NO MEMORY\n");

            getch();

       }


       unsigned short dmask = 31;

       unsigned short mmask = 480;

       unsigned short ymask = 65024;


       newfile->theDate.day = fnode->ff_fdate & dmask;

       newfile->theDate.month = fnode->ff_fdate & mmask;;

       newfile->theDate.year = fnode->ff_fdate & ymask;


       newfile->theDate.month =  newfile->theDate.month >> 5;
       newfile->theDate.year =  newfile->theDate.year >> 9;

       newfile->theDate.year += 1980;     // base year is 1980, DOS


       return newfile;


}


      // convert file dates from conventional to DOS format for comparison -  dateSort()

FBLKPTR convert_to_DOS_date(FBLKPTR *newfileDate)
{

          FBLK *dos_file;


      int *conv[3], SUM;


      dos_file =  *newfileDate;

      dos_file->theDate.year -= 1980;    // DOS, base year = 1980

      dos_file->theDate.year *= 512;

      dos_file->theDate.month *= 32;

      conv[0] = &dos_file->theDate.day;

      conv[1] = &dos_file->theDate.month;

      conv[2] = &dos_file->theDate.year;

      SUM = *conv[0] + *conv[1] + *conv[2];

      dos_file->ff_fdate = (unsigned short)SUM;    // typecast SUM from int to unsigned short

      return dos_file;

}

           // convert file time from conventional to DOS format, add_File()

FBLKPTR convert_to_DOS_time(FBLKPTR *newfileTime)
{


          FBLK *dos_file;


      int *conv[3], SUM;


      dos_file =  *newfileTime;

      dos_file->theTime.hour *= 2048;

      dos_file->theTime.minute *= 32;

      dos_file->theTime.sec /= 2;

      conv[0] = &dos_file->theTime.hour;

      conv[1] = &dos_file->theTime.minute;

      conv[2] = &dos_file->theTime.sec;

      SUM = *conv[0] + *conv[1] + *conv[2];

      dos_file->ff_ftime = (unsigned short)SUM;      // typecast SUM from int to unsigned short


      return dos_file;

}

                // convert file time from DOS to conventional format for display

FBLKPTR convert_from_DOS_time(FBLK *f_Time)
{


          FBLK *nodeTIME;
      F_TIME ft;

      nodeTIME = new FBLK;

      if(nodeTIME == NULL) {

                puts("\nERROR, NO MEMORY!!\n");
             getch();

             exit(1);

      }

      unsigned short smask = 31;

      unsigned short mmask = 2016;

      unsigned short hmask = 63488;

      nodeTIME->theTime.sec = f_Time->ff_ftime & smask;      // ffblock.ff_ftime was received from main at function
        nodeTIME->theTime.minute = f_Time->ff_ftime &mmask;      // call, it is stored in int f_Time, so f_Time is used
        nodeTIME->theTime.hour  = f_Time->ff_ftime & hmask;      // here instead of fblock.ff_ftime.

        nodeTIME->theTime.minute = nodeTIME->theTime.minute >> 5;
        nodeTIME->theTime.hour  = nodeTIME->theTime.hour >> 11;

           ft = ConvertClock_to_AmPm(nodeTIME->theTime);

      nodeTIME->theTime = ft;


        return nodeTIME;

}



F_TIME ConvertClock_to_AmPm(F_TIME fileTime)
{

      F_TIME newTime;


      if(fileTime.sec >= 60) {

                fileTime.minute += fileTime.sec / 60;

              fileTime.sec  = fileTime.sec % 60;

      }

      newTime.sec = fileTime.sec;

      if(fileTime.minute >= 60) {

            fileTime.hour += fileTime.minute / 60;

            fileTime.minute = fileTime.minute % 60;

      }

      newTime.minute = fileTime.minute;


      if(fileTime.hour <= 12) {

           newTime.hour = fileTime.hour;

           strcpy(newTime.Am_Pm, "AM");

       }

       else {


           newTime.hour = fileTime.hour;

           strcpy(newTime.Am_Pm, "PM");

       }

      return newTime;

}


void printfiles(FBLK *dhptr)
{

           FBLK *newfile, *d, *t;

           newfile = dhptr;


           if(newfile == NULL) {


              puts("\nLIST IS EMPTY !!\n");

            getch();

            return;

         }


         puts("\n\nFILE  NAME      FILE-SIZE        FILE-DATE         FILE TIME");
           puts("------------------------------------------------------------\n");
         while(newfile != NULL) {

              d  = convert_from_DOS_date(newfile);

              t  = convert_from_DOS_time(newfile);

                 printf("\n%s\t %d\t %d:%d:%d\t%d:%d:%d%s\n", newfile->ff_name,
                                             newfile->ff_fsize,

                              d->theDate.day,
                              d->theDate.month,
                              d->theDate.year,

                              t->theTime.hour,
                              t->theTime.minute,
                              t->theTime.sec,
                              t->theTime.Am_Pm);


                   newfile = newfile->next;

                  }

             puts("\n\n\n\n\n\t\t\tPRESS ENTER TO CONTINUE !!\n\n");

             getch();

         return;

}

-Y
Avatar of yunikon

ASKER

ps,
     I've verified that the file added (menu 7) has been inserted into the link list and has also entered nameSort().

-y
do you have to sort yourself? You can call qsort()

void nameSort(FBLK *List)
{
   FBLK *prev, *curr, *tempptr, *NewNode;
   char order;
   int pass;

   tempptr      = List;
   prev           = tempptr;
   curr           = tempptr->next;

     NewNode = (FBLK *)malloc(sizeof(FBLK));
     if(NewNode == NULL) {
          puts("\nERROR, NO MEMORY\n");
         getch();
         return;
   }
   int Length = listLength(List);
   order = listOrder();
   for (pass = 1; pass <= Length; pass++)
   {
        // Loop to compare one fileName at a time with fileNames in other Nodes
        while(1)
        {
            int name_cmp = strcmp(prev->ff_name, curr->ff_name);
             if((order == 'a' && name_cmp > 0) || (order == 'd' && name_cmp < 0))
             {
                      // Store previous information in the NewNode temporarily
                    strcpy(NewNode->ff_name, prev->ff_name);
                    NewNode->ff_fsize = prev->ff_fsize;
                    NewNode->ff_fdate     = prev->ff_fdate;
                    NewNode->ff_ftime     = prev->ff_ftime;
                      // Store the next node data in the previous Node
                      strcpy(prev->ff_name, curr->ff_name);
                     prev->ff_fsize = curr->ff_fsize;
                     prev->ff_fdate     = curr->ff_fdate;
                     prev->ff_ftime     = curr->ff_ftime;
                      // Store saved data from the NewNode to the next Node
                      strcpy(curr->ff_name, NewNode->ff_name);
                      curr->ff_fsize = NewNode->ff_fsize;
                      curr->ff_fdate     = NewNode->ff_fdate;
                      curr->ff_ftime     = NewNode->ff_ftime;
            }
             prev = curr;
             curr = curr->next;
             if(curr == NULL)
             {
                    prev = tempptr;
                    curr = tempptr->next;
                    break;
              }
         }
   }
    List = prev;
    while(List != NULL) {
               List = List->next;
     }
}

Function looks ok ... but if it is not called enough number of times, sorting will not be complete ... Are you aure you are getting the correct length of the list ?
>unless the filename is the last or highest
Makes me feel that the loop is running one time too less ... also it is also possible to modify the loop a bit to have bubble sort kind of implementation ... right now there are some avoidable cases being looped through
Hi yunikon,

strictly seen your code isn't C-code because you use the c++ keyword "new" in this function.
Secondly, you don't use "directory" in the following function. You always open drive "a:\".
Is this correct?

------8<-----------
void find_Files(FBLKPTR *head, char directory[])
{
          int done;

      struct ffblk fblock;

      FBLK *node, *d, *t;

      node = new FBLK;                      /*            <-- NEW here !?!? */

        if(node == NULL)  puts("\nERROR, NO MEMORY\n");

      *head = node;

        printf("\n\tDirectory listing of %s\n\n", directory);

      done = findfirst("a:\*.*", &fblock, NORMAL);

       while(!done) {
------8<-----------

and I found a mistake in this snippet of code:

----------8<---------------
   switch(choice) {

            case 1:

              puts("\n\nENTER NAME AND PATH OF FOLDER  -> [a for A drive] ?\n\n");

              scanf("%s", &path);                   /*  <--- HERE */

              find_Files(&head, path);

              printfiles(head);

              break;
----------8<---------------

should this not be  scanf("%s\n", &path);       ?

regards
void_main
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yunikon

ASKER

Sorry for my lack of communicaion.    

sunnycoder:  08/03/2004 12:12am PDT->  i've verified that the list length is correct.

snnycoder:  08/04/2004 09:44PM PDT->  yes, efficiency of bubble sort can be improved with a little modification.

void main:  08/09/2004 10:45PM PDT-> found out about
the 'new' keyword when I put the code in a Borland project and 'new' was showing up as an error.   Had to change them all and use malloc().  Keyword 'new' does work with C in Borland non-projects though.   Your'e correct there is an error there.  Variable 'path'  was not declared in function find_Files.  scanf("%s", &path)  should have been  scanf("%c", &path)  since path is intended to be a character ('a'  for A drive).  

I eventually got the code to work and found the following error in function name_Sort;

  strcmp() is case sensitive so it would determine that 'Red' is smaller than or should precede 'add'.  I therefore used the non case sensitive function strcmpi() to compare the filenames;

   for (pass = 1; pass <= Length; pass++)
   {
        // Loop to compare one fileName at a time with fileNames in other Nodes
        while(1)
        {
            int name_cmp = strcmp(prev->ff_name, curr->ff_name);

             if((order == 'a' && name_cmp > 0) || (order == 'd' && name_cmp < 0))
             {

               
There is one issue that I have not been able to fixed so far.  After creating the linked list in function find_Files and then printing the list an extra file is always printed at the end of the list and all its data are 0's.  If I print the files during the creation of the linked list the extra file does not appear at end of list.  The added file is appearing after exiting the 'while(!done) {' loop (which creates the linked list) in function find_Files.  I suspect that this has something to do with the file pointer of the read directory and end of file.
To correct this I exited the  [ while(!done) {   loop]  if findnext() returns -1.  This prevents the creation of a new node when the end of the directory  is reached.  You can check this out by removing  [ if (done == -1)  break;
else { ]    and running the code;

      done = findnext(&fblock);
      if(done == - 1)    // exit loop and prevent creation of following node if end of file
             break;
      else {
      node->next = new FBLK;   // create new node if new file found
      if(node->next == NULL) {
           puts("\nERROR, NO MEMORY !!\n");
           getch();
           return ;
      }
      node = node->next;
     }
   }
   node->next = NULL;

 

Avatar of yunikon

ASKER

void main:  08/09/2004 10:45PM PDT-> yes I always use drive A.