?
Solved

new at c...simple database program question

Posted on 2003-03-17
13
Medium Priority
?
170 Views
Last Modified: 2010-04-15
Is there any simple database programs tutorials or snippets on the web?
Also, What I'm trying to do is create a database:

Options:
1: Initialize from a file
2: Add a record
3: Display records
4: Remove a record
5: Change a record
6: Write to a file
0: Quit

typedef struct {
     char first[10];
     char last[20];
     char addr[40];
     int age;
     int id;
}my_rec_t;

and we're suppose to this struct to do it.  I just need suggestions on how to start this and maybe an already made program so that I can base this program on.  I'm not asking for code..just helpful hints, Thanks.
0
Comment
Question by:gcman03
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8152901
I don't know of any "pre-made" code for you.  Here are some suggestions:

You will want either a linked list, or an array of these structs.  An array is easier, but not really what you would likely use in a real-world program.

Create functions that will read and write the structs to a file.  I would suggest that you simply write the strings and ints separately to the file, and not the structs so that it is portable.

Display shoul be similar to write, just printf to the screen, instead of fprintf to a file.

To remove a record, if you use a linked list you can simply delete that node (don't forget to update the pointer to the next struct). In an array, you can use memmove to "suck" the following structs up one place.

Give it a start and post if you have questions.
0
 

Author Comment

by:gcman03
ID: 8153025
can someone tell me specfically how to initialize from a file...like how to read stuff from a .txt file?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8153116
FILE *myFile;
char buffer[100];

myFile = fopen("C:\mydata.txt", "r"):

fgets(buffer, sizeof(buffer), myFile);

This will read 1 line, or 99 characters, into buffer - with a null terminator
0
Industry Leaders: 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!

 
LVL 6

Accepted Solution

by:
gj62 earned 300 total points
ID: 8153171
However, if you are reading and writing fixed data, you may be better off doing the following:

Open the file as above.

Then have two functions:

readData(my_rec_t myStruct)
{
  fread(myStruct.first, sizeof(my_rec_t.first, 1, myFile);
  fread(myStruct.last, sizeof(my_rec_t.last, 1, myFile);
  fread(myStruct.addr, sizeof(my_rec_t.addr, 1, myFile);
  fread(myStruct.age, sizeof(my_rec_t.age, 1, myFile);
  fread(myStruct.id, sizeof(my_rec_t.age, 1, myFile);
}

writeData(my_rec_t myStruct)
{
  fwrite(myStruct.first, sizeof(my_rec_t.first, 1, myFile);
  fwrite(myStruct.last, sizeof(my_rec_t.last, 1, myFile);
  fwrite(myStruct.addr, sizeof(my_rec_t.addr, 1, myFile);
  fwrite(myStruct.age, sizeof(my_rec_t.age, 1, myFile);
  fwrite(myStruct.id, sizeof(my_rec_t.age, 1, myFile);
}

Obviously, you need to do some error checking in a real program...
0
 
LVL 6

Expert Comment

by:gj62
ID: 8153479
oops - bug...

in the line for both read and write for ID, I incorrectly take the size of age.  Since they are both ints, it will work, but you should change it in case you modify either one in the future.
0
 

Author Comment

by:gcman03
ID: 8154476
readData(my_rec_t myStruct)
{
 fread(myStruct.first, sizeof(my_rec_t.first, 1, myFile);
 fread(myStruct.last, sizeof(my_rec_t.last, 1, myFile);
 fread(myStruct.addr, sizeof(my_rec_t.addr, 1, myFile);
 fread(myStruct.age, sizeof(my_rec_t.age, 1, myFile);
 fread(myStruct.id, sizeof(my_rec_t.age, 1, myFile);
}

so what the above would do is read each line from myfile, whatever myfile....my question is how does it know what to read...for example if the file its taking has a first line of :
lastname,firstname,address,age,ID
How will the fread know that lastname will go to my_rec_t.last...and etc.

Thanks for your help!
0
 
LVL 6

Expert Comment

by:gj62
ID: 8154506
That's the reason that you "pair" your read and write functions.  Calling readData reads in one strucutre.  Calling writeData writes out one structure.  You can think of this as your database "records".  It really isn't reading lines in the file, since the file has no line breaks.  The format is a fixed length, binary (because of the ints) format.

If the file is ONLY modified by those two functions, you will be fine.

Now, if you want to read and write in ASCII only, you can do that.  You can have a fixed length format, or a delimited format.  Let me know if this is what you want.

0
 

Author Comment

by:gcman03
ID: 8154566
Ok...sorry, I am a little new at this.  So the program will write out initally using the fwrite function.  The next time..the program will read the file using fread and it will know that say...my_rec_t.last is a certain part of the binary file by ???   Just wondering how the file will know that a certain part of the binary file will be my_rec_t.last...also..if there are multiple database record entries...how should that be handled?

Initially...when the user runs the program...all the data he enters should be temporarily stored in memory...and then he has the option to write it to file...next time he wants it..he'll just initlaize that file and the file will contain all the records previously saved.  So my questions I quess is how C will read the file...specfically how it will just read certain parts of the file and store them back into memory...say my_rec_t.last and if there are multiple records...how C handles it.  Thanks
0
 
LVL 6

Expert Comment

by:gj62
ID: 8154598
That's the reason that you "pair" your read and write functions.  Calling readData reads in one strucutre.  Calling writeData writes out one structure.  You can think of this as your database "records".  It really isn't reading lines in the file, since the file has no line breaks.  The format is a fixed length, binary (because of the ints) format.

If the file is ONLY modified by those two functions, you will be fine.

Now, if you want to read and write in ASCII only, you can do that.  You can have a fixed length format, or a delimited format.  Let me know if this is what you want.

0
 

Author Comment

by:gcman03
ID: 8154624
uh...was that a typo?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8154628
Sorry for the copied posts - I'm thinking slow today...

Anyhow C won't "know" - you, the programmer, have to make it happen.

The above method works if the ONLY thing you store in the file are records that are written to the file using the writeData function.  If someone edits the file, or stores other information in the file, the program can't correctly read the data.

So, if you start with a blank file, the user can then enter records and write them to disk.  You can then read the records back from disk into memory - either in an array of structures, or into a linked list.

Does this help?
0
 

Author Comment

by:gcman03
ID: 8154667
Ahh I see, so the program will only read files it creates or edits...
and that function does that..by taking the info from the structs(which contains the info) and saves them to a file.  Should the file be saved in binary or text file? or does it matter?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8154744
Well, technically to C there is no difference, but if you are using Microsoft, they have a "text" file mode (also called translated mode) that tries to handle EOL (carriage return-line feed pairs) for you.

In this case, since it really isn't a text file, binary is just fine.

0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

762 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