?
Solved

database question

Posted on 2003-03-19
9
Medium Priority
?
183 Views
Last Modified: 2010-04-15
Hi
I am creating a simple database that opens, prints, views, deletes records from a file.

My question is how do you delete a certain record from the database?

The database is stored in a struct
so like
arr[i].name
arr[i].stuff.....

so lets say user wantst to delete record i=23...whats the process to do it?  Thanks!
0
Comment
Question by:generalya_deo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Author Comment

by:generalya_deo
ID: 8171569
also...another question lol
when adding to the record
should i use fgets or scanf?....note some of the strucutres are char..and some of it are integers

and if fgets...can someone explain fgets...thx!
0
 

Author Comment

by:generalya_deo
ID: 8171589
and yet another question..ROFL....
when i'm adding records
lets say i'm adding record arr[i+1] where i is the last record in database...should i allocate memory for it before i do?
0
 

Author Comment

by:generalya_deo
ID: 8171644
and yet another question..ROFL....
when i'm adding records
lets say i'm adding record arr[i+1] where i is the last record in database...should i allocate memory for it before i do?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:generalya_deo
ID: 8171647
     printf("Enter Last:\n");      
      fgets(last, 10, stdin);
      strcpy(arr[size+1].last,last);
      printf("Enter First:\n");
      fgets(first, 20, stdin);
      strcpy(arr[size+1].first,first);
      printf("Enter Address:\n");
      fgets(addr, 40, stdin);
      strcpy(arr[size+1].addr,addr);
      printf("Enter Age:\n");
        scanf("%d",&arr[size+1].age);
        scanf("%c", &c);
      printf("Enter Id:\n");
        scanf("%d",&arr[size+1].id);
        scanf("%c", &c);

is this proper?
0
 
LVL 6

Accepted Solution

by:
gj62 earned 300 total points
ID: 8171651
In order:

1)If you are going to be working with a reasonable number of records (more than 100, but they still all fit in memory), your best bet is a linked list, which makes deletions easy and efficient.  If not, you can use memmove, which physically shifts your array's memory.

Let's say you have an array of numStruct structures called myStructArray - if you want to delete the 4th structure (array offset 3), you would write:

memmove(&myStructArray[3], &myStructArray[4], numStruct-4);
numStruct-=1;

2) Really your choice, but you should not write structs to a file (non-portable, and due to structure padding might change between compiler versions, though unlikely).  To be sure, create a writeStruct() and readStruct() function to write the structures as strings (or ints, or other appropriate types, as you prefer).  If you are unclear about this, let me know and I can show you an example from a question posted earlier this week.

3) ABSOLUTELY.  If you don't allocate more memory, your program will eventually crash.  If you currently have numStruct elements in your array, you could allocate more by using realloc(), e.g.

myStructArray = realloc(myStructArray, sizeof(struct)*(numStruct+1));  

You can do this as often as you want, but it is not efficient since it might have to move the entire array in memory, and could fail as it requires contiguous space.  Much better to use a linked list...
0
 

Author Comment

by:generalya_deo
ID: 8171683
my_rec_t *stuipd;
stuipd=&arr[size+1];


scanf("%c", &c);

      printf("Enter Last:\n");      
      fgets(last, 10, stdin);
      strcpy(stuipd->last,last);
      printf("Enter First:\n");
      fgets(first, 20, stdin);
      strcpy(stuipd->first,first);
      printf("Enter Address:\n");
      fgets(addr, 40, stdin);
      strcpy(stuipd->addr,addr);
      printf("Enter Age:\n");
        scanf("%d",&stuipd->age);
        scanf("%c", &c);
      printf("Enter Id:\n");
        scanf("%d",&stuipd->id);
        scanf("%c", &c);

i'm trying to add a record...but all this is doing is overwritting records...what i'm i doing wrong?
size is just the number of records
0
 
LVL 3

Expert Comment

by:TascoDLX
ID: 8172094
Are you incrementing your 'size' variable?

Should probably be:

  stuipd=&arr[++size];

Or, if you want to store data in the zero-element of your array (arr[0]) and still have an accurate 'size':

  stuipd=&arr[size++];


0
 
LVL 20

Expert Comment

by:jmcg
ID: 10184845
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by gj62.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question