Thread: C programming challenge

  1. #1
    lost in C
    Guest

    Question C programming challenge

    Hey everyone!
    The description of the program is on this page:
    http://www.red-white.com/program.htm

    I'm having problems with some functions.
    I need some help how to do the 'append', 'update', and 'compact' functions.

    Any help will be greatly appreciated.
    Thank you in advance.

    P.S. Just in case if it matters, this is to be run in Linux (gcc compiler).

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post some code. It's better usually to show us a brief attempt; any error messages you're getting, or where you're generally stuck at.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    lost in C
    Guest
    I just do not know where to start yet.
    I'm completely lost.

  4. #4
    lost in C
    Guest
    So far I have this:

    ------------ a3.c begins ---------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "diskdb.h"

    int main(int argc, char**argv){

    char choice;
    int nColumns, i;
    int colType;
    table_hdr myHdr;
    FILE* myFile;

    myFile=fopen(argv[1], "r+");
    if(myFile==NULL){
    myFile=fopen(argv[1], "w+");
    printf("Please input number of columns (max 6):\n");
    do{
    scanf("%d", &nColumns);
    if(nColumns>6){
    printf("Too many columns, try again:\n");
    }
    else if(nColumns<0){
    printf("Too few columns, try again:\n");
    }
    }while((nColumns<0)||(nColumns>6));

    /*while((colType>=0)&&(colType<3))*/

    /*if((colType<0)||(colType>2)){
    printf("Invalid data type, try again:\n");
    }
    else{*/
    for (i=0;i<nColumns;i++){
    printf("Enter type (0/INT, 1/FLO, 2/STR) and title for Column[%d]:", i);
    scanf("%d %s", &colType, myHdr.title[i]);
    printf("%d\n", colType);
    if((colType<0)||(colType>2)){
    printf("Invalid data type, try again:\n");
    }
    }
    /*}*/
    }
    else{
    fread(&myHdr, sizeof(myHdr),1,myFile);
    }

    switch(choice = menu()){
    case 'a':
    append(&myHdr, myFile);
    break;
    case 'b':
    update(&myHdr, myFile);
    break;
    case 'd':
    delete(&myHdr, myFile);
    break;
    case 'c':
    compact(&myHdr, myFile);
    break;
    case 'p':
    print(&myHdr, myFile);
    break;
    case 'q':
    quit(&myHdr, myFile);
    break;
    default:
    }

    return 0;

    }

    ------------ a3.c ends ---------------


    ------------ diskdb.c begins ---------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "diskdb.h"

    char menu(){
    char op;

    printf("Data Base Utilities:\n");
    printf("a: append a new record\n");
    printf("u: update an existing record\n");
    printf("d: delete a record\n");
    printf("c: compact the database file\n");
    printf("p: print the database file\n");
    printf("q: quit the program\n");
    printf("Please enter your choice: ");
    scanf("%c", &op);
    while (op == ' ' || op == '\n') scanf("%c", &op);
    return op;
    }

    void quit(table_hdr* table, FILE* myFile){
    fclose(myFile);
    return;
    }

    void append (table_hdr* table, FILE* myFile){

    return;
    }

    void update (table_hdr* table, FILE* myFile){

    return;
    }

    void delete (table_hdr* table, FILE* myFile){

    return;
    }

    void compact (table_hdr* table, FILE* myFile){

    return;
    }

    void print (table_hdr* myHdr, FILE* myFile){

    int i,j;

    for (i=1;i<myHdr->cols;i++){
    for (j=0;j<=20;j++){
    printf("-");
    }
    printf("+");
    }

    return;
    }

    ------------ diskdb.c ends ---------------


    ------------ diskdb.h begins ---------------
    #define STRING_LEN 20
    #define COLUMN 6
    #define TITLE_LEN 20
    typedef enum {INT, FLO, STR} datatype;

    typedef struct {
    datatype col_type[COLUMN];
    int col_offs[COLUMN];
    int cols;
    int row_size;
    char title[COLUMN][TITLE_LEN];
    } table_hdr;

    char menu();
    int ftruncate( int, long );
    int fileno( FILE * );
    void append (table_hdr*, FILE* myFile);
    void update (table_hdr*, FILE* myFile);
    void delete (table_hdr*, FILE* myFile);
    void compact (table_hdr*, FILE* myFile);
    void print (table_hdr*, FILE* myFile);
    void quit (table_hdr*, FILE* myFile);

    ------------ diskdb.h ends ---------------

    I am stuck. Please help.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    This looks like a hard one, but i will have a go, if you get a good answer yourself post something so that I don't waste too much time, we will get a doctorate out of this one for sure.

  6. #6
    lost in C
    Guest
    I almost got 'apend' and 'print' functions working.

    Here's whatr I have:

    void append (table_hdr* myTable, FILE* myFile){
    /*-
    The append function adds a row to a table.
    It then writes the column data to the binary file at the end of the file.
    -*/
    int i;
    int inInt;
    float inFloat;
    char inStr[STRING_LEN];

    for (i=0;i<myTable->cols;i++){
    printf("Input %s: ", myTable->title[i]);
    if(myTable->col_type[i]==INT){
    scanf("%d", &inInt);
    fwrite(&inInt,sizeof(int),1,myFile);
    }
    else if (myTable->col_type[i]==FLO){
    scanf("%f", &inFloat);
    fwrite(&inFloat,sizeof(float),1,myFile);
    }
    else{
    scanf("%s", inStr);
    fwrite(&inFloat,sizeof(char)*STRING_LEN,1,myFile);
    }

    }
    return;
    }

    void print (table_hdr* myTable, FILE* myFile){
    /*-
    This function prints the rows of the table.
    -*/
    int i,j;
    for (i=0;i<myTable->cols;i++){
    printf("%20s", myTable->title[i]);
    if ((i+1)<myTable->cols){
    printf("|");
    }
    }
    printf("\n");
    for (i=1;i<=myTable->cols;i++){
    for (j=0;j<20;j++){
    printf("-");
    }
    if (i<myTable->cols){
    printf("+");
    }
    }
    printf("\n");

    return;
    }

    My print function won't print out the appended rows though. I'm trying to find out how to do it.
    And in my output file I don't get what I should (1's and 0's) instead I get some crap. I think that there's a problem in my 'append' as well.

  7. #7
    lost in C
    Guest
    Can anybody help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AI Challenge
    By unknown_111 in forum General AI Programming
    Replies: 0
    Last Post: 10-02-2007, 12:18 AM
  2. A Challenge in C++
    By Brad0407 in forum C++ Programming
    Replies: 38
    Last Post: 07-18-2007, 12:56 PM
  3. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:56 AM
  4. Challenge?
    By Mackology101 in forum C Programming
    Replies: 5
    Last Post: 11-23-2004, 02:30 PM
  5. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM