• C

program problem

Here is the database:

32
Name SID Phone Grade
dick,arrea 00001 301-888-9000 90
lawrence,aaron 00002 333-111-2222 85
allen,abbey 01238 353-680-7559 78
alexander,abbi 19086 396-999-6670 99
allison,abe 99534 395-585-9950 50
milk,abigail 89745 301-879-2204 32
aro,absalom 99134 333-124-9978 45
smith,acacia 03021 456-456-3354 89
jackson,crawford 12456 890-909-5432 0
frank,cree 99324 997-376-7445 11
conrad,creghton 87095 898-980-7865 33
cressidia 88451 958-907-2007 1
crystal,crispian 04656 889-995-9023 2
skycaptain,curt 90746 907-989-8876 3
google,curtis 89745 917-990-9917 99
stewart,cuthbert 90775 996-913-9765 99
taylor,daley 89756 145-225-2012 90
kok,ebony 99134 301-888-9988 78
stanley,ed 49321 777-357-9910 54
lee,gae 23451 790-758-8090 4
lee,gaenor 12355 780-905-7918 23
glenn,korey 59478 765-117-8891 5
gilbert,kori 88902 759-896-9928 60
a,kurtis 09234 898-750-9875 61
gordon,kyler 13457 909-987-7890 64
b,kym 89075 123-345-8876 65
neo,megan 09156 112-775-3346 77
neil,meghan 99017 257-434-2254 2
grace,mel 89744 907-879-9912 99
curtis,toni 09114 890-905-6789 97
newton,topaz 99165 991-663-4678 15
oliver,zula 78451 898-224-1544 40

This the program that I have started:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "p3FileRead.h"

#define NUM_FIELDS 4
#define MAX_FIELD_LENGTH 20

typedef struct{
  char name[20], phone[12];
  int SID, grade;
}Student_t;


int NUM_STUDENTS ;
int FILE_SIZE ;
int choice;



int main()
{
    char* file ;
 file = enee114_readfile() ; //read inputfile
                                // you may use file as a character array
                                // of length FILE_SIZE.
    printf("%s", file) ;

  //***********************************************************
  NUM_STUDENTS = atoi(file) ;
  FILE_SIZE = strlen(file) ;

  printf("There are %d student. The filesize is %d bytes\n",
         NUM_STUDENTS, FILE_SIZE) ;

  Student_t **StudentList = NULL; //Array of student records
  int count = 0, i= 24, j = 0;    //Number of records in the StudentList array
  char arr[21];

  //scanf("%d",&NUM_STUDENTS);
  while(file[i] != ' ')
  printf("%c", file[i++]);

  printf(" ***Main*** \n ");   //print main menu
  printf("1. Search \n");
  printf("2. Sort\n");
  printf("3. Advance Search\n");
  printf("4. Exit\n");

  scanf("%d", & choice);
  if(choice == 1)
    {
    printf(" ***Search***"\n "); // print search menu
    printf(" 1.Name  \n");
    printf(" 2.SID \n");
    printf(" 3.Phone \n");
    printf(" 4.Grade \n");
    printf(" 5.Exit \n");
      scanf("%d", & choice);
      if(choice == 1)
        {
          for(int i= 0; i<FILE_SIZE; i++)
            {
              printf("%c", file[i]);
            }
        }
    }
 return 0 ;
}





 This program  is suppost  to allow the user to be be able to search, sort or advance search in the database by either name SID, phone # or grade. It is a good start I guess. I tried to run part of the first have but it is not working properly. Is there any suggestion that somebody can give me so I can fix this problem and be able to continue on it with no problem??

Ofakile1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

griesshCommented:
Hi Ofakile1,
> but it is not working properly
What is not working?

Your fist problem seems to be in:
 char* file ;
 file = enee114_readfile() ; //read inputfile
 printf("%s", file) ;
 NUM_STUDENTS = atoi(file) ;
 FILE_SIZE = strlen(file) ;


What does enee114_readfile() do? Does it allocate for a char array and return the pointer of that char array?
What do you expect  printf("%s", file) does?
Why do you think you can convert a file descriptor to an int?
Why do you think that 'file' is a string?

=====
Werner
0
baboo_Commented:
Hey there -

Post the code (or preferably just the method signature and description) for enee114_readfile()  and we can answer your question.

baboo_
0
charukalanidhiCommented:
Hi there,

If the database is in a file, i really dont understand how you are trying to access the file. As far as i know, i can give a small code which will work for your first problem.

suppose the database is present in a file called "details.txt". this is how i would end up programing.

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

/* Make sure you enter the structure in the same order as the data in the database */

typedef struct{
  char name[20];
  int SID;
  char phone[12];
  int grade;
}Student_t;



main()
{
      FILE *fp;
      int choice,serch_id;
      char search_string;
      
      Student_t namelist;
      
      if((fp=fopen("details.txt","r")) ==NULL)
      {
            puts("cannot open file");
            exit();
      }

      printf(" ***Search***"\n "); // print search menu
      printf(" 1.Name  \n");
      printf(" 2.SID \n");
      printf(" 3.Phone \n");
      printf(" 4.Grade \n");
      printf(" 5.Exit \n");
      scanf("%d", &choice);
      
      switch(choice)
      {
      case 1:
      {
            printf("Enter the name:");
            scanf("%s",search_string);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(strcmp(namelist.name,search_string)==0)
                  {
                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);
                  }
            }
      }
      break;
      case2:
      {
            printf("Enter the SID:");
            scanf("%d",search_id);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(namelist.SID == search_id)
                  {

                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);
                  }
            }

      }
      break;
      
      case 3:
      {
            printf("Enter the phone:");
            scanf("%s",search_string);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(strcmp(namelist.phone,search_string)==0)
                  {
                        printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);                        
                  }
            }
      }
      break;
      case4:
      {
            printf("Enter the grade:");
            scanf("%d",search_id);
            printf("The details of the person you searched for are:\n");
            while(fread(&namelist,sizeof(Student_t),1,fp) ==1)
            {
                  if(namelist.grade == search_id)
                  {
                              printf("%s,%d,%s,%d\n ",namelist.name, namelist.SID, namelist.phone,namelist.grade);            
                  }
            }

      }
      break;
      default:
            fclose(fp);
            exit();
      }
}


Hope this helps with what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.