• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

new at c...simple database program question

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
gcman03
Asked:
gcman03
  • 8
  • 5
1 Solution
 
gj62Commented:
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
 
gcman03Author Commented:
can someone tell me specfically how to initialize from a file...like how to read stuff from a .txt file?
0
 
gj62Commented:
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
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
gj62Commented:
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
 
gj62Commented:
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
 
gcman03Author Commented:
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
 
gj62Commented:
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
 
gcman03Author Commented:
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
 
gj62Commented:
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
 
gcman03Author Commented:
uh...was that a typo?
0
 
gj62Commented:
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
 
gcman03Author Commented:
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
 
gj62Commented:
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

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now