C/C programming Continuation
Expert: Zlatko - 11/10/2010
QuestionThanks for your response. The use of structures could be useful. However, i don't seem to understand how exactly to use structures. Ive read several tutorials about structures and unions. But they dont seem to show how to fill in the variables in the structure with input from the user then transferring this info to a file.
The data base programm im working on should be able to hold names and some background info about the person. Such as age, contacts experience and history.
My trouble is how can i create the console interface that can recieve input from the user then write it to a file for future retrieval.
What format should the data be stored on a file. For example i've heard of comma separated data. Could that help? But i donot know how to implement it into my program. The user should also be able to enter a persons name as a query and his/corresponding information will be retrieved from the file and displayed on the screen.
As you suggested, i will consider the login security feature last. I appreciate all your help.
AnswerYou can create a console interface with simple printf statements to prompt the user for input, and scanf statements to read the input.
For example, have a function to present the user with a menu and ask for a menu choice, and then return the choice to the main program, like this.
int getOperation()
{
int choice;
printf("Operations menu\n");
printf("1. Add to the database\n");
printf("2. Find a record in the database\n");
printf("3. Print all records in the database\n");
printf("4. Exit program\n");
printf("Enter your choice >");
scanf("%d", &choice);
return choice;
}
The above code is very simple, and doesn't check for bad choices or bad user input, but you get the general idea.
In main, you would do something like this
int main()
{
while(1)
{
int choice = getOperation();
switch(choice)
{
case 1:
handleAddOperation();
break;
/* other cases go here */
case 4:
exit(0);
}
}
}
I have not run the above code through a compiler so there may be syntax errors, but you get the general idea. You need to present the user with choices and then act on the choices. All the code of your program should be packaged into small functions that do only one logical part of the program. Your main program should be very clean and short and it should delegate operations to functions.
Structures are a simple part of the C language, so unless you tell me explicitly that you don't want to use them, I'll assume that you are willing to learn something new. Structures will make it easy to read from and write to the file because an entire record can be handled in one line. Comma separated variables will require a little more programming to parse out the fields of the database. And you will need to handle the case of a comma being part of the data, such as in the person's history. It is a trade-off you have the new headache of learning structures, but you get some relief because other parts of the program become simpler. Anyway, I will show you how to use them.
Here is a sample structure
typedef struct dbRecord
{
char name[30];
int age;
} DbRecord
Say you obtained a some name and age from somewhere. To put them into a stucture you would do something like this:
DbRecord record; /* this creates a record variable */
strcpy(record.name, someName);
record.age = someAge;
Then you can write the entire structure out to some file like this:
fwrite(&record, /* write the address of the record */
sizeof(DbRecord), /* the number of bytes in a record */
1, /* the number of records to write */
someFile); /* the FILE to write to */
Similarly when reading, you can read an entire record at once.
DbRecord record;
fread(&record, sizeof(DbRecord), 1, someFile);
then you can access the record, for example to print
printf("The name is %s\n", record.name);
printf("The age is %d\n", record.age);
Notice there is no code to worry about how thing are laid out in the file. They are laid out in the DbRecord format.
Of course, you should consult with your instructor about all of this and remember that the code I've written may have some spelling errors, because I did not run it through a compiler.
There is one disadvantage of using structures, and that is that every record in your database will be of fixed size. You will need to decide how many characters to allow for the name, and for any other strings, and then you are stuck with that length. You cannot increase the length in your program, and hope to read back a file made with an older version of the program. Generally, beginner assignments do not worry about such things, but if your assignment requires high flexibility about the lengths of names and histories, then you will need to do some more clever programming. Let me know.
If you are required to write a report about your design, you should mention the advantages and disadvantages of whatever approach you choose so your instructor knows that your choices were deliberate.