Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Building a "database" in C, need help

database files c/c++ rigestration id degree name student search delete

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 07 November 2013 - 05:40 AM

i wanna make a simple database using files in c language

will store in file

1.student id

2.student degree

3.student name

will store it like this

id         degree            name

the main menu is like the following

1.create a new file

2.add new student

3.display student (search by id)

4.search student (search by id)

5.delete student  (search by id)

6.view all student

7.exit

the file will be like the following

id             degree              name

32            66                     john

23            71                     harry

41            89                     peter

 


Edited by ScareCrawExtended, 07 November 2013 - 05:43 AM.


#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 07 November 2013 - 05:41 AM

Okay, so what's your question?


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 07 November 2013 - 06:16 AM

the problem is i know how to program c put i didnt reached the files in c and am supposed to finish this assignment before 10/11/2013 so i could use some help "need help".



#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 07 November 2013 - 08:03 AM

What do you have so far? Do you understand what your algorithm will look like? Do you know how to read/write a file?

 

You've given us no code, so we have no idea where you are stuck. It could be anywhere between displaying the menu of options to deciding on how to store the information in memory to...

 

Right now, I have no idea what, specifically, you are having an issue with.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 07 November 2013 - 10:32 AM

here is my code i have problem with the search and delete and update  method search and delete and update are by student id
#include <stdio.h>
void add();
void view();
void search();
void display();
void del();
void update();
int main(void)
{
int x;
do{
   printf("Main Menu:\n1.create new file\n2.add student\n3.display student\n4.search student\n5.remove student\n6.view all students\n7.exit\n");
   scanf("%d",&x);
switch(x){
case 1:
break;
case 2:
      add();
break;
case 3:
           display();
    break;
    case 4:
           search();
    break;
    case 5:
      del();
    break;
    case 6:
           view();
    break;
    case 7:
    break;
    default:
printf("wrong choice");
    break;
}
}while(x!=7);
   return 0;
}
/****************************************************/
void add()
{
int id;
int degree;
char name[50];
printf("Enter student id\n");
scanf("%d",&id);
printf("Enter student degree\n");
scanf("%d",&degree);
printf("Enter student name\n");
scanf("%s",name);
FILE*f2=fopen("st.txt","a");
fprintf(f2,"%d %d %s\n",id,degree,name);
fclose(f2);
}
/****************************************************/
void view()
{
FILE * fp;
       int    degree,id;
       char   name[50];
fp = fopen( "st.txt" , "r");
        printf("id\tdegree\tname\n");


if (fp) {
    while (fscanf(fp, "%d\t%d\t%s",&id,&degree,name)!=EOF){
          fprintf(stdout, "%d\t%d\t%s\n",id,degree,name);}
    fclose(fp);
}
}
/****************************************************/
void search(){
FILE*fp=fopen("st.txt","r");
int id;
int id1;
printf("Enter student id\n");
scanf("%d",&id);
if (fp) {
    while (fscanf(fp, "%d",&id1)!=EOF){
if(id!=id1){
          printf("notFound\n");}
          else{
printf("Found\n");
 }


    fclose(fp);
}
}


}
/****************************************************/
void display(){






}
/****************************************************/
void del(){






}
/****************************************************/
void update(){






}
/****************************************************/

 


Edited by Roger, 07 November 2013 - 11:32 AM.
added codetags


#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 07 November 2013 - 12:26 PM

For add(), can a student be entered twice? What checks should be in place?

 

I would consider loading the file into three arrays in memory. This would allow you to delete/add/update records as array manipulations, and then write those arrays back to file.

 

Once that is done, display is just a matter of printing id[index], degree[index], name[index]. Deleting becomes a "delete and shift up". Update is just simple array access.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 Thor

Thor

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 07 November 2013 - 01:22 PM

To fix search()  you need to

  • move fclose out of the while-loop
  • fscanf the whole line, not just the id-field

Like this:

void search()
{
  FILE* fp = fopen("st.txt","r");
  int degree, id, id1;
  char name[50];
  int found = 0;

  printf("Enter student id ==> "); scanf("%d", &id);

  if (fp)
  {
    while (fscanf(fp, "%d %d %s", &id1, &degree, name) != EOF)
    {
      found = id == id1;
      if (found) break;
    }    
    fclose(fp);
  }
  printf(found ? "Found\n" : "Not found\n");
}

The delete() function could load all lines *except the one with the id to delete* into a temp file, delete the original file and rename the temp file.

I'll leave the coding to you - healthy little exercise... ;)

 



#8 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 07 November 2013 - 01:52 PM

i know to make a string in c you use char array but if i want to make an array of strings what to do???



#9 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 08 November 2013 - 12:31 AM

i have a bit of problem with the del method i've put the file into three arrays to do the delete but it wont work

void del(){
int i=count(),j,k;
int key,l;
int *id[i],id1;
int *degree[i],degree1;
char *name[i],*name1;
FILE* fp = fopen("st.txt","r");
//load file into arrays
for(int y=0;y<i;y++){
fscanf(fp,"%d%d%s",&id1,&degree1,name1);
degree[y]=degree1;
id[y]=id1;
name[y]=name1;
//printf("%d\t%d\t%s\n",id[y],degree[y],name[y]);
}

printf("Enter student id\n");
scanf("%d", &key);
l=i; //Length of the array
//delete the wanted items from the array
for(k=0;k<l;k++)
{
if(id[k]==key)
{
for(j=k;j<l;j++)
id[j]=id[j+1];
degree[j]=degree[j+1];
name[j]=name[j+1];
l--; //Decreasing the length of the array
}
fclose(fp);


}
FILE* fp1 = fopen("st.txt","w");
//save the new array to file
for(int y=0;y<i-1;y++){
fprintf(fp1,"%d%d%s",id[y],degree[y],name[y]);
}
fclose(fp1);
}

Edited by ScareCrawExtended, 08 November 2013 - 12:54 AM.


#10 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 08 November 2013 - 01:05 AM

i know to make a string in c you use char array but if i want to make an array of strings what to do???

 

Use an array of char pointers:

 

char ** array_of_string = (char **)malloc(sizeof(char *)* n);

Creating SEGFAULTs since 1995.


#11 ScareCrawExtended

ScareCrawExtended

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 08 November 2013 - 10:09 AM

guys need help somtin wrong in the loop but i cant figure it out

the program .exe is terminated as soon as i run the program

//delete the wanted items from the array
for(k=0;k<l;k++)
{
if(id[k]==key)
{
for(j=k;j<l;j++)
id[j]=id[j+1];
degree[j]=degree[j+1];
name[j]=name[j+1];
l--; //Decreasing the length of the array
}

Edited by ScareCrawExtended, 08 November 2013 - 10:11 AM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download