Sunday 1 September 2013

Sale Data Saved To Disk

I just noticed today that fwrite and fread is heck of a lot easier than using fprintf and fscanf. Dang! Wish I had known before.

Here is a program that stores or displays the Name, Amount Sold, Sale Price and the Date & Time for a particular sale.

NOTE: Even if you do not enter anything, if you run the program it will create a folder called "MyDaily" on your C Drive. Just FYI, so you know it's safe to delete and not one of those "*Maybe* an important system file" kind of file.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void date(char *da);
void times(char *da);

main()
{
    FILE *fp;
    char tm[30],dt[30],c;
    int rw;
    struct daily
    {
        char name[40];
        int NumberSold;
        double SalePrice;
        char time[50];
    };
    struct daily a;

    fp=fopen("C:\\MyDaily\\Sale.dat","rb"); //test to see if this file exists
    if(fp==NULL) //does not open, it possibly does not exist
    {
        /*create the "mydaily" directory, if it exists, the 2>nul prevents DOS from
        freaking out and displating a message like "folder exists"*/
        system("md C:\\MyDaily 2>nul");
        fp=fopen("C:\\MyDaily\\Sale.dat","wb"); //create the sale.dat file
        if(fp==NULL)
        {
            printf("Unable to create data file.");
            getch();
            exit(0);
        }
    }
    fclose(fp); //close the file in read or write mode

    printf("Do you want to read or write?(R/W)");
    c=toupper(getch(c));
    if(c=='R')
    {
        rw=1;
    }
    else if(c=='W')
    {
        rw=2;
    }

    while(rw==1)
    {
        fp=fopen("C:\\MyDaily\\Sale.dat","rb"); //open in read mode.

        if(fp==NULL)
        {
            printf("Cannot Open File1");
            exit(1);
        }
        while(fread(&a,sizeof(a),1,fp)==1)
        {
            printf("\n%s %i %.2wf %s",a.name,a.NumberSold,a.SalePrice,a.time);
        }
        break;
    }

    while(rw==2)
    {
        fp=fopen("C:\\MyDaily\\Sale.dat","ab"); //open in append mode.
        if(fp==NULL)
        {
            printf("Cannot Open File2");
            exit(2);
        }

        system("cls");
        /*clears the buffer of old values. Comment this out. Run the program, enter something
        when it asks if you want to enter another, say yes. You'll see why this is here*/
        fflush(stdin);
        printf("Enter Name Of Item: ");
        gets(a.name);
        printf("Enter Number Sold: ");
        scanf("%i",&a.NumberSold);
        printf("Enter Sale Price: ");
        scanf("%wf",&a.SalePrice);
        date(dt);
        times(tm);
        sprintf(a.time,"%s%s",dt,tm);
        fwrite(&a,sizeof(a),1,fp);
        printf("Enter Another?(Y/N): ");
        c=toupper(getch(c));
        if(c=='Y')
        {

        }
        else if(c=='N')
        {
            system("cls");
            break;
        }
    }
    fclose(fp);
    return 0;
}

void date(char *da) //Displays date when needed.
{
    time_t now;
    struct tm*d;
    char li[30],li1[30],*dt;

    time(&now);
    d=localtime(&now);
    strftime(li,30,"DT%d-%m-%y",d);
    dt=&li;
    while(*dt!='\0')
    {
        *da=*dt;
        da++;
        dt++;
    }
    *da='\0';
}

void times(char *t) //Displays Time when needed.
{
    int x;
    time_t now;
    struct tm*d;
    char li[30],li1[30],*tm;

    time(&now);
    d=localtime(&now);
    strftime(li1,30,"TM%H:%M:%S",d);
    tm=&li1;
    while(*tm!='\0')
    {
        *t=*tm;
        t++;
        tm++;
    }
    *t='\0';
}

No comments:

Post a Comment