Searching + Deleting from databases in C?


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

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Sys_ProgConnect With a Mentor Commented:
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

ankuratvbConnect With a Mentor Commented:
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.

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.

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.


All Courses

From novice to tech pro — start learning today.