You are here:

C/C programming

Advertisement


Question
Hi, im learing C programming. Im working on a school project that requires me to create a simple data base software that can store names and requires a username-password login system. The program should be able to allow new entries which can then be appended to the file.
I was wondering how could i use files in C to acheive this project. Please suggest any approaches that i could use. Thanks you in advance.

Answer
Hello

Yes you can use C to achieve this project. You need to think about what will be stored in the database. Usually a database stores a key and a value. The key is the information you know and the value is the information you want to get. For example, say you wanted to store names and their telephone number and then find a telephone number based on the name. In this example, the name is the key, and the phone number is the value. In your question you say you want to store names. Are you sure that you don't want to store some value associated with the name?

Once you decide what you want to store, create a structure to package the data. In my example, I could have a structure like this.

struct DbEntry
{
   char firstname[30];
   char lastname[30];
   char telephone[12];
}

Then next thing you want to decide are the operations on the database. Usual operations are "add an entry" "find an entry" and "delete an entry". You would have a separate function for each operation.

For adding an entry you would ask the user for the name and phone number, place it in the DbEntry record, and write the record to the disk.

For finding an entry, you would read through the file, reading data into struct DbEntry until the desired name is found.

The struct DbEntry helps maintain the structure of the file. It helps to ensure that each record in the file is the same size. It allows the entire record to be read from, or written to, the file at once.

Finally you will need some sort of user interface to ask the user for operations and data and password. I suggest you leave this for the last part and start by creating and testing your add/search/delete functions. I suggest you leave the user interface to the end because it is very tedious to test your software by asking the user for input all the time. It is much easier to test your functions by calling them from main with data hard-coded into the program.

To read and write from files, you will need to know how to use
fopen, fread, and fwrite, and fclose
you can read about them, and find examples here
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
http://www.cplusplus.com/reference/clibrary/cstdio/fread/
http://www.cplusplus.com/reference/clibrary/cstdio/fwrite/
http://www.cplusplus.com/reference/clibrary/cstdio/fclose/

I hope that helps you to start thinking about your project. Your question is very general and so I have kept the answer very general. As you think about your project, you will have more specific questions. Feel free to ask.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.