All pastes #739617 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-17 10:23 UTC
rendered paste body
// task3a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void get_field(FILE *ptr, char *name, int length);

int _tmain(int argc, _TCHAR* argv[]) {
	//initialising/declaring variables
	char cust_num[5];
	char comp_name[36];
	char abn[14];
	char street[37];
	char suburb[25];
	char state[8];
	char post_code[10];
	char phone[14];
	char sales_num[5];
	char descrip[46];
	char part_no[9];
	char unit_cost[12];
	char day_of_month[8];
	FILE *cust_in, *sales_in;
	cust_in=fopen("C:\\Users\\Luigerr\\Desktop\\PP2\\Level 3\\customer.txt", "r");
	int i;
	//for loop used as number of customers is known
	for(i=1;i<11;i++) {
		//moves the 'cursor' to the required position in the customer.txt document
		int m = (135 * i);
		fseek(cust_in,m,SEEK_SET);
		
		//reads from the customer.txt into seperate arrays
		get_field(cust_in, cust_num, 3);
		get_field(cust_in, comp_name, 34);
		get_field(cust_in, abn, 12);
		get_field(cust_in, street, 35);
		get_field(cust_in, suburb, 23);
		get_field(cust_in, state, 6);
		get_field(cust_in, post_code, 8);
		get_field(cust_in, phone, 12);

		//printing out customer details in required format
		printf("TOF ---------\n\n\n");
		printf("ABN: %s\n", abn);
		printf("%s\n", comp_name);
		printf("%s\n", street);
		printf("%s\t %s\t %s\n\n", suburb, state, post_code);
		printf("%s\n\n\n\n", phone);

		int cust_key=atoi(cust_num);
		//opens sales.txt
		sales_in=fopen("C:\\Users\\Luigerr\\Desktop\\PP2\\Level 3\\sales.txt","r");
		float total = 0;
		//loop to read through sales.txt and enter details into array
		for(int k=1;k<200;k++) {
			//positons cursor
			int u= k * 72;
			fseek(sales_in,u,SEEK_SET);
			get_field(sales_in, sales_num, 3);
			//convert sales key
			int sales_key=atoi(sales_num);
			//if sales keys match enter into array
			if (sales_key == cust_key) {
				get_field(sales_in, descrip, 44);
				get_field(sales_in, part_no, 7);
				get_field(sales_in, unit_cost, 10);
				get_field(sales_in, day_of_month, 6);
				//convert price
				float price = atof(unit_cost);
				printf("%s %s $ %1.2f \t%s/08/06 \n", descrip, part_no, price, day_of_month);
				total += price;
			}
			//rewinds cursor
			fseek(sales_in,0,SEEK_SET);
		}
		printf("\t\t Total Invoice: %1.2f \n\n", total);
		fclose(sales_in);
		fseek(cust_in,0,SEEK_SET);	//closes customer.txt so that the 'cursor' can be moved into and appropriate position
	}

	


	fclose(cust_in);

	return 0;
}


void get_field(FILE *ptr, char *name, int length)
{
   fread(name, length, 1, ptr);   
   name[length]='\0';
   
   return;
}