473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Input/Output and Files

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[i].id);
printf("Item name is: %s\n", items[i].name);
printf("Item cost is: %.2f\n", items[i].cost);
printf("Item category is: %s\n", &items[i].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[i].id, items[i].name,
items[i].cost, &items[i].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
Nov 13 '05 #1
0 1473

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2944
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
5
6983
by: Clemens Park | last post by:
Hi, I am working on a compressor/decompressor for files, and it works great at the moment with text files, but not great all with binary files. The problem is that it reads in binary files as...
7
7521
by: MM | last post by:
Hi there, How can I change my code (below) so that I use an "input argument" to specify the file name of the input file? For example, if I compile the code and that the application then gets the...
1
1830
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
8
2664
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
3
6087
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
2
1569
by: gopala | last post by:
Hi, I am pretty new to python but i do have experience with c++. As a part of learning exercise i wrote a python script to insert beautifying comments(with name, problem...) to our lab program...
1
3303
by: terminatorul | last post by:
Hello Sorry if asking a known question. I have a program that reads lines of text from standard input and writes the non-empty ones back to standard output. In most cases it will be used with...
3
4523
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
1
1578
by: randysimes | last post by:
I have a program to read in words and sort those words into lexicographical order. The problem I have is that it reads the input file endlessly. I know it does not reach the output section because...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.