🔒 Closed Need help guys

Status
Not open for further replies.

honor

Fanatic
Guys pahelp naman po paano po gumawa ng File delete and file update Mula po dito code ko, if pwede pacomment nalang din po Yung code, tia po! Baguhan palang kasi ako.

C++:
void creatf();
void dispf();
void searchf();

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<io.h>
#include<string.h>

typedef struct info
  {
   char id[5];
   char name[30];
   float gpa;
  }info;
info record[100];

int fp, amode, permission;
int v;
char av;
char d_gpa[5];
char find_id[5];
char sel;

main()
  {
   do
    {
     clrscr();
      printf("MAIN MENU \n\n");
       printf("1. Create File \n");
    printf("2. Display File \n");
     printf("3. Search File \n");
      printf("4. EXIT \n\n");
      printf("Please select: ");
       sel=getche();

     switch(sel)
      {
       case '1':
    clrscr();
     creatf();
    getche();
    break;
       case '2':
    clrscr();
     dispf();
    getche();
    break;
       case '3':
    clrscr();
     searchf();
    getche();
    break;
       case '4':
    clrscr();
    getche();
    break;
      }
    }while(sel!='4');
     getche();
  }

//------- begin -------
void creatf()
{
   clrscr();
    amode=O_RDWR|O_CREAT|O_TRUNC|O_TEXT;
    permission=S_IREAD|S_IWRITE;
    fp=open("MASTER.TXT", amode, permission);

  if(fp!=NULL)
   {
    v=0;
    do
    {
     clrscr();
      printf("Enter ID: ");
       gets(record[v].id);
    fflush(stdin);
      printf("Enter Name: ");
       gets(record[v].name);
    fflush(stdin);
     printf("Enter GPA: ");
      fgets(d_gpa, sizeof(d_gpa), stdin);
       record[v].gpa=atof(d_gpa);
     printf("Do you want to continue? [Y/N]: ");
      av=getche();
       fflush(stdin);
      write(fp, &record, sizeof(record));
     v++;
   }while((av!='N')&&(av!='n'));
  }
  else
   {
    printf("Can't open file");
    getche();
   }
  close(fp);
  getche();
}
//------- end -------

//------- begin -------
void dispf()
  {
   v=0;
    clrscr();
     gotoxy(27,1);printf("Displaying the records \n\n");
      gotoxy(1,4); printf("ID");
       gotoxy(11,4); printf("Name");
    gotoxy(41,4); printf("GPA");
     fp=open("MASTER.TXT",O_RDONLY|O_TEXT);
      while(!eof(fp))
       {
    read(fp,&record,sizeof(record));
     gotoxy(1,5+v); printf("%s", record[v].id);
      gotoxy(11,5+v); printf("%s", record[v].name);
    sprintf(d_gpa,"%.2f",record[v].gpa);
       gotoxy(41,5+v); printf("%s", d_gpa);
    v++;
       }
     gotoxy(27,20); printf("Nothing follows!!!");
    close(fp);
    getche();
  }
//------- end -------

//------- begin -------
void searchf()
  {
   v=0;
    clrscr();
     printf("Search From The Master File \n\n");
      printf("Type ID to search: ");
       gets(find_id);

     amode= O_RDONLY|O_TEXT;
     fp=open("MASTER.TXT", amode);

     while(!eof(fp))
      {
       read(fp, &record, sizeof(record));
    if(((strcmp(find_id, record[v].id))==0))
     {
      clrscr();
       gotoxy(6,5); printf("ID");
        gotoxy(16,5); printf("Name");
         gotoxy(42,5); printf("GPA");
       gotoxy(6,7); printf("%s", record[v].id);
        gotoxy(16,7); printf("%s", record[v].name);
       sprintf(d_gpa, "%.2f", record[v].gpa);
        gotoxy(42,7); printf("%s", d_gpa);
      break;
     }
    else
     {
      clrscr();
       printf("No Records Found");
     }
       v++;
      }
     close(fp);
    getche();
  }
//------- end -------
 
honor Your code snippet is DIFFICULT to read. A code that's difficult to read means:
  • it's hard to reason out
  • hard to troubleshoot
  • hard to test
  • leads to eventual downfall
Can you simplify the code first and encapsulate similar things first? Probably create a Github repo so we can run your app.

Instead of having these garbled implementation on your main function:
Code:
main()
  {
   do
    {
     clrscr();
      printf("MAIN MENU \n\n");
       printf("1. Create File \n");
    printf("2. Display File \n");
     printf("3. Search File \n");
      printf("4. EXIT \n\n");
      printf("Please select: ");
       sel=getche();

     switch(sel)
      {
       case '1':
    clrscr();
     creatf();
    getche();
    break;
       case '2':
    clrscr();
     dispf();
    getche();
    break;
       case '3':
    clrscr();
     searchf();
    getche();
    break;
       case '4':
    clrscr();
    getche();
    break;
      }
    }while(sel!='4');
     getche();
  }

Split them up and extract similar behavior on a separate function or Object.
Code:
main()
{
    do
    {
        clrscr();
        printmenu();
        processSelection();
    }
}

Which one is easier to read? The former or the latter? Now if you're asking someone to troubleshoot your code, I'd bet the latter would be more helpful to the troubleshooter.
 
Remember the Gang of Four principle:
  • "Program to an 'interface', not an 'implementation'." (Gang of Four 1995:18)
  • You do not have permission to view the full content of this post. Log in or register now.: "Favor 'You do not have permission to view the full content of this post. Log in or register now.' over 'You do not have permission to view the full content of this post. Log in or register now.'." (Gang of Four 1995:20)
You do not have permission to view the full content of this post. Log in or register now.
 
Status
Not open for further replies.

About this Thread

  • 5
    Replies
  • 442
    Views
  • 4
    Participants
Last reply from:
pixkit

Trending Topics

Online now

Members online
1,119
Guests online
1,427
Total visitors
2,546

Forum statistics

Threads
2,280,014
Posts
28,994,728
Members
1,226,566
Latest member
bossjames21
Back
Top