Thread: Need help creating a database in C

  1. #1
    Registered User phoneix's Avatar
    Join Date
    Mar 2012

    Unhappy Need help creating a database in C

    Hi everyone
    I am new to C and new to this forum as well.
    I need to create a student database which stores the name and roll number of the student.
    I have almost no knowledge of how to create a database in c.
    the program below is what i tried to do but honestly i don't understand what it actually does.
    I can't understand if the name and roll number is replaced every time i press y or if it creates entirely a different file or record or something.
    I also want to edit the database whenever i want but i don't see anything in the program that can help me edit the database i have created.
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    void main()
    FILE *a;
    int sno;
    char sname[20],any[1];
    printf("Enter Student Number : ");
    printf("Enter Student Name : ");
    printf(a,"%d %s \n",sno,sname);
    printf("Do you wish to continue(Y/N)");
    I would appreciate any help on creating a database.

  2. #2
    Registered User
    Join Date
    Sep 2007
    It's going to be a text flat file. The function to output is typically:

    fprintf(fileptr, "%d $s\n", sno, sname);

    I'd also recommend you use a more meaningful variable name for the file pointer other than 'a'.

  3. #3
    Registered User phoneix's Avatar
    Join Date
    Mar 2012
    Thanks Cynic
    I think i understand the basics of how this program works.
    but still i want to be able to modify the entries i made before
    what extra codes i need to write to be able to read the contents of the file and make changes accordingly.

  4. #4
    Registered User
    Join Date
    Jan 2009
    You need to store you previous entries in a data structure. The simplest approach is to read in all entries into an array at start up, it's quite limited but simple. A more flexible method is to use something dynamic like a linked list.

    Create a struct student with name and roll number, you can use that regardless.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Some suggestions:

    1) change void main() to int main(void), and add a return 0; at the end of the program. This is something your OS will expect.

    2) above int main() prototype a struct and it's members inside it, so all your functions can know what that is.

    3) also above int main() declare an array of those structs you just prototyped, so you have the array in global memory, instead of in local stack memory.

    4) to simplify things, remember that memory is cheap these days. Make your array size a #define above int main(), and make it 20% larger than you ever expect you'll need it to be. Yes, it's wasteful - and yes, it's also smart.

    5) load all your database (which will be a 'simple' flat file), into your array, every time the program starts, and then close that file. You'll open the file again with updates (edits), as needed. Leaving a file open can cause a loss of data if the power is lost suddenly.

    Don't ask me how I know this.

    6) when a record is deleted, you don't really delete it - you zero out the name and the id number. Avoid any unnecessary "shuffling" of data.

    7) include an "int idx[SIZE]" array. That will be used for sorting your data, whichever way you want to (according to name, age, id number, major, grade point average, home room number, adviser, etc.), all WITHOUT having to move any data at all.

    8) if your student number gets too large, you'll adjust the size of the #define SIZE N where N is the size of the array. New records are appended onto the end of the data file, in the order they are entered. The idx array handles the display of sorted values for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating the Database Using C++
    By Faiza Iqbal in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2011, 01:28 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Creating a database (inside a GUI)
    By goosematt in forum C Programming
    Replies: 7
    Last Post: 10-23-2003, 11:04 AM
  4. Creating a simple database: is my approach OK?
    By m712 in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2002, 02:27 AM
  5. creating a small database need some hints
    By asim0s in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2002, 10:44 AM

Tags for this Thread