Improve company productivity with a Business Account.Sign Up


Searching + Deleting from databases in C?

Posted on 2004-04-07
Medium Priority
Last Modified: 2010-04-15

I want to search a database/text file/master record by Surname, Post-code and/or Email Address, and also delete and entry from a master file yet i do not know how.

I know how to print ALL of the selected fields in a master file, yet not search, for example...


               printf ("Address    : %s\n", contacts[i].address1);
                  printf ("             %s\n", contacts[i].address2);
                  printf ("             %s\n", contacts[i].address3);
                  printf ("PostCode   : %s\n", contacts[i].postcode);
                  printf ("Home Number: %s\n", contacts[i].homenumber);
                  printf ("Mob. Number: %s\n", contacts[i].mobnumber);
                  printf ("Email      : %s\n", contacts[i].email);


Any help/examples are appreciated

Question by:ryb2001
  • 2
LVL 10

Accepted Solution

Sys_Prog earned 500 total points
ID: 10774830
This seems to be a hoemwork sort of question

Hence cannot give u the code , but can Definitely help

U need to define a struct having the same format as that of a record in your file
Open the file [fopen()]
read one record at a time from the file  [fread()]
Compare the individual elemtnts of the read record using strcmp () OR other comparison techniques [depending on datatype of elements]

For Deletion,
U need to decide the strategy
U can either delete the whole record, In that case u need to somehow utilise the space emptied by the deleted record [either by inserting a new record if required OR moving forward by size of one record the data after the deleted record ]

U can also have logical deletion [maintaiing a delete flag with each record to indicate whether a particular record is deleted. This technique would prevent moving the data back and forth BUT leads to wastage of space in case u have many deletions. U can utilize the space occupied by deleted records by overwriting them at a later stage while inserting new records


Assisted Solution

ankuratvb earned 500 total points
ID: 10775170
Besides Linear Search,if ur records are large in no. and u are likely to search quite often,
U can use binary search as well.

The problem u'll have is Binary search requires sorted records.
Now u can sort on one field in the record while storing.

So,the field on which u'll be searching the most,keep the records sorted on that field.

The advantage u gain by binary search is quite a lot.

For deletion also,Amit covered most of ur options.

U have to make the choice whether size is more imp. or speed .
If speed,u can use the delete flag and not physically delete the record.
Physical deletion can be done at peiodic intervals on use request.

If space is ur concern,then physical deletion and moving all subsequent records is the best option.


Expert Comment

ID: 10775404
U can use fwrite() to write the entire structure i.e. one record in one statement rather than having to write one field at a time.


Expert Comment

ID: 10775512
Hello Ryan,

If you are comfortable in DB programming using C on windows then you can also try the ODBC route.
Even for text files there are Text drivers available where once you connect and open a datasource
it is very trivial to perform queries and searches.

The effort for writing parsing and searching logics would be less but then it has its own challenges.



Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

589 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