A
AKF
I am trying to use data structures to open a file (void
display_Stock_Item(Stock_Item si) from a text file. I also need to
be able to write to the file and also to delete from the file using
arrays. Can anyone help?
/*Declare header files to be used in the program*/
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
const int SIZE=20;
//Declaring structures to be used in the program
struct Stock_Item;
struct Bill;
struct Bill_Item;
struct Stock;
struct Stock_Item //Function prototype for a Stock data type
{
int code;
char description[25];
float price;
};
struct Stock
{
Stock_Item si[SIZE];
int nStock_Item;
};
struct Bill_Item //Function prototype for a Bill_Item data type
{
int code;
char description[25];
float weight;
float cost;
};
struct Bill //Function prototype for a Bill data type
{
Bill_Item bi[SIZE];
int nBill_item;
};
//Function Prototypes available to the data structure
void add_Bill_Item(Bill_Item& bi, int* code, char* desc, float* w,
float* c);
void set_Stock_Item (Stock_Item& si, int* code, char* desc, float* p);
void display_Bill_Item(Bill_Item bi);
void display_Stock_Item(Stock_Item si);
void create_Stock_Item(Stock_Item& si);
void create_Stock(Stock& s);
int isfull(Stock_Item& si);
//initialising Stock Items to zero for the array
void create_Stock(Stock& s)
{
s.nStock_Item = 0;
}
//Setting stock_Item
void set_Stock_Item(Stock_Item& si, int code, char desc, float p)
{
cout << "Enter a code:";
cin >> si.code;
cout << "Enter a description:";
cin >> si.description;
cout << "Enter a price:";
cin >> si.price;
}
//Display Stock_Item structure operations
void display_Stock_Item(Stock_Item &si, int c, char d, float p)
{
si.code = c;
si.description[SIZE] = d;
si.price = p;
cout << si.code << si.description << si.price <<endl;
}
//Setting Bill_Item
void add_Bill_Item(Bill_Item& bi)
{
cout << "Enter a code:";
cin >> bi.code;
cout << "Enter a description:";
cin >> bi.description;
cout << "Enter a weight:";
cin >> bi.weight;
cout << "Enter a cost:";
cin >> bi.cost;
}
//Display Bill_Item Structure Operations
void display_Bill_Item(Bill_Item bi)
{
cout << bi.code << bi.description << bi.weight <<bi.cost <<endl;
}
//*****************THE ACTUAL PROGRAM STARTS HERE**************
void main()
{
// Create a pointer of type FILE and initialise to NULL
// FILE is a data structure in stdio.h
FILE *per_file = NULL;
char temp;
// Open the file for reading
// Parameter 1 = filename and path if required
// Parameter 2 = r (read mode)
per_file = fopen("C:\\prices.txt","r");
//While temp is equal
while
((temp=fgetc(per_file))!=EOF)
cout<<temp;
// Always close a file after use
fclose(per_file);
}
display_Stock_Item(Stock_Item si) from a text file. I also need to
be able to write to the file and also to delete from the file using
arrays. Can anyone help?
/*Declare header files to be used in the program*/
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
const int SIZE=20;
//Declaring structures to be used in the program
struct Stock_Item;
struct Bill;
struct Bill_Item;
struct Stock;
struct Stock_Item //Function prototype for a Stock data type
{
int code;
char description[25];
float price;
};
struct Stock
{
Stock_Item si[SIZE];
int nStock_Item;
};
struct Bill_Item //Function prototype for a Bill_Item data type
{
int code;
char description[25];
float weight;
float cost;
};
struct Bill //Function prototype for a Bill data type
{
Bill_Item bi[SIZE];
int nBill_item;
};
//Function Prototypes available to the data structure
void add_Bill_Item(Bill_Item& bi, int* code, char* desc, float* w,
float* c);
void set_Stock_Item (Stock_Item& si, int* code, char* desc, float* p);
void display_Bill_Item(Bill_Item bi);
void display_Stock_Item(Stock_Item si);
void create_Stock_Item(Stock_Item& si);
void create_Stock(Stock& s);
int isfull(Stock_Item& si);
//initialising Stock Items to zero for the array
void create_Stock(Stock& s)
{
s.nStock_Item = 0;
}
//Setting stock_Item
void set_Stock_Item(Stock_Item& si, int code, char desc, float p)
{
cout << "Enter a code:";
cin >> si.code;
cout << "Enter a description:";
cin >> si.description;
cout << "Enter a price:";
cin >> si.price;
}
//Display Stock_Item structure operations
void display_Stock_Item(Stock_Item &si, int c, char d, float p)
{
si.code = c;
si.description[SIZE] = d;
si.price = p;
cout << si.code << si.description << si.price <<endl;
}
//Setting Bill_Item
void add_Bill_Item(Bill_Item& bi)
{
cout << "Enter a code:";
cin >> bi.code;
cout << "Enter a description:";
cin >> bi.description;
cout << "Enter a weight:";
cin >> bi.weight;
cout << "Enter a cost:";
cin >> bi.cost;
}
//Display Bill_Item Structure Operations
void display_Bill_Item(Bill_Item bi)
{
cout << bi.code << bi.description << bi.weight <<bi.cost <<endl;
}
//*****************THE ACTUAL PROGRAM STARTS HERE**************
void main()
{
// Create a pointer of type FILE and initialise to NULL
// FILE is a data structure in stdio.h
FILE *per_file = NULL;
char temp;
// Open the file for reading
// Parameter 1 = filename and path if required
// Parameter 2 = r (read mode)
per_file = fopen("C:\\prices.txt","r");
//While temp is equal
while
((temp=fgetc(per_file))!=EOF)
cout<<temp;
// Always close a file after use
fclose(per_file);
}