Input/Output and Files

Discussion in 'C Programming' started by David, Jul 22, 2003.

  1. David

    David Guest

    Could someone help me turn this program into a program that accepts
    files(database) as an input and writes to a file as method of saving
    the database information?

    #include <stdio.h>

    #define n 25
    #define m 10

    struct item{
    int id;
    char name[n];
    float cost;
    char category;
    } items[m];
    int x=0;

    int find(int id){
    int j;
    for(j=0; j<x; j++){
    if(items[j].id==id)
    return j;
    }
    return -1;
    }

    void add(void){
    int id;

    if(x==m){
    printf("Sorry too many items in database!\n");
    return;
    }

    printf("Enter item id: ");
    scanf("%d", &id);
    items[x].id=id;
    if(find(id) >=0){
    printf("Item prexists entry attempt.\n");
    return;
    }

    printf("Please enter the product's name: ");
    scanf("%s", items[x].name);
    printf("What is the price of the item: $");
    scanf("%f", &items[x].cost);
    printf ("Please enter the category for the item:\n"
    "\tM=MEAT\n"
    "\tP=PRODUCE\n"
    "\tD=DAIRY\n"
    "\tC=CANNED FOODS\n"
    "\tN=NONFOODS\n");
    scanf("%s", &items[x].category);
    x++;
    return;
    }

    void deletes(void){
    int id, j, k;
    printf("\nEnter item id: ");
    scanf("%d", &id);
    j=find(id);
    if(j>=0){
    for(k=j;k<x;++k)
    items[k]=items[k+1];
    }
    else{
    printf("Part Not Found in This Database!\n");
    }
    x--;
    return;
    }

    void edit(void){
    int id;
    printf("Please enter the item number: ");
    scanf("%d", &id);
    if(find(id) >=0){
    printf("Please enter the new cost of the item: ");
    scanf("%f", &items[20].cost);
    }
    else
    printf("Item not found!\n");
    return;
    }

    void search(void){
    int i, id;
    printf("Please enter the item number: ");
    scanf("%d", &id);
    i=find(id);
    if(i>=0){
    printf("\nItem id is: %d\n", items.id);
    printf("Item name is: %s\n", items.name);
    printf("Item cost is: %.2f\n", items.cost);
    printf("Item category is: %s\n", &items.category);
    }
    else
    printf("Item was not found!\n");
    }

    void print(void){
    int i;
    printf("Item Id"
    "\t\tItem Name"
    "\tItem Cost"
    "\t\tItem Category\n");

    printf("_____________________________________________________________________________\n");
    for(i=0; i<m; i++)
    printf("%7d\t\t%s\t\t%.2f\t\t\t%s\n\n", items.id, items.name,
    items.cost, &items.category);
    }

    int main(void){
    int choice;
    printf("\n\n\tTHE GROCERY STORE DATABASE!\n\n");
    for( ; ; ){
    printf("Enter the number of your choice:\n"
    "\t1.\tADD ITEM.\n"
    "\t2.\tDELETE ITEM.\n"
    "\t3.\tEDIT ITEM (CHANGE COST).\n"
    "\t4.\tSEARCH FOR ITEM.\n"
    "\t5.\tDISPLAY DATABASE CONTENTS.\n"
    "\t6.\tQUIT.\n");
    printf("\t");
    scanf("%d", &choice);
    while(getchar()!= '\n');
    switch(choice){
    case 1: add();
    break;
    case 2: deletes();
    break;
    case 3: edit();
    break;
    case 4: search();
    break;
    case 5: print();
    break;
    case 6:
    return 0;
    default:
    printf("Try Again!\n");
    }//end switch
    }//endfor
    return 0;
    }//end main
     
    David, Jul 22, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    4
    Views:
    386
    Alex Vinokur
    Aug 25, 2003
  2. Aalok
    Replies:
    1
    Views:
    317
    Tim Hagan
    Sep 17, 2004
  3. input output files

    , Oct 24, 2005, in forum: C++
    Replies:
    3
    Views:
    386
    Karl Heinz Buchegger
    Oct 24, 2005
  4. malla
    Replies:
    9
    Views:
    403
    Eric Pruneau
    Aug 9, 2006
  5. Ron Brennan
    Replies:
    5
    Views:
    344
    Dr John Stockton
    May 14, 2004
Loading...

Share This Page