Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
void invoiceFile();
int main(void){
invoiceFile();
return 0;
}
void invoiceFile(){
FILE *ordPtr;
ordPtr = fopen("orders.txt", "r");
FILE *invPtr;
invPtr = fopen("inventory.txt", "r");
while(!feof(invPtr)){
int n, addNum, zip, itemselect, quantity;
char x[28];
int item;
char itemName[28], firstName[28], lastName[28], address1[28], st[28], city[28], state[28];
int cost, inventory;
fscanf(invPtr, "%d, %s, %d, %d", item, itemName, cost, inventory);
srand(time(NULL));
n = rand () % 1000;
while(!feof(ordPtr)){
fscanf(ordPtr, "%d %s %s %d %s %s %s %s %d %d", itemselect, firstName, lastName, addNum, address1, st, city, state, zip, quantity);
if(itemselect == item){
int invPtr;
sprintf(x, "invoice%d.txt", n);
FILE *invoice;
invoice = fopen(x, "w");
fprintf(invoice, "Invoice for:\n%s %s\n%d %s %s\n%s %s %d\n\nItem# product qty price each\n%-8d%-21s%-6d%-10d\n total = $%d\n", firstName, lastName, addNum, address1, st, city, state, zip, item, itemName, quantity, cost, quantity*cost);
}
}
}
}
It's a work in progress, so style will be updated, etc. and there is more to flesh out the code. However, I am getting a segmentation fault and I couldn't pinpoint exactly where it was since I'm unfamiliar with the syntax and using/creating files. The idea is that it reads from an inventory.txt file and an order.txt file and compiles an invoice. Each invoice file will have a unique and random name.