Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

database question

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
generalya_deo
Asked:
generalya_deo
1 Solution
 
generalya_deoAuthor Commented:
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
 
generalya_deoAuthor Commented:
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
 
generalya_deoAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
generalya_deoAuthor Commented:
     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
 
gj62Commented:
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
 
generalya_deoAuthor Commented:
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
 
TascoDLXCommented:
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
 
jmcgOwnerCommented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now