0

So basically I've got this project in C - I have to design a customer/order system, in which the customer can make multiple orders but each order is limited to a single product type. I created a struct 'orders' which contains the customer ID, product name, price, and the order time. Here is the code to calculate time:

        // computing order time
        time_t timeorder;
        char * displayT;
        timeorder = time(NULL);
        // convert time to string so that it can be displayed
        displayT= ctime(&timeorder);
        printf("Your order time is: %s\n", displayT); //check if time displays correctly
        newOrders11[count13].timeorder = displayT;

Immediately after, the struct 'orders' is written to file. After the user makes an order, he/she should be available to view his/her LATEST order after entering his/her ID number. However, when the order is read from the file, the first order the user made (rather than the latest) is displayed. Here is the code for reading from file:

 while (count13<=MAXORDERSTOBEMADE && (fread(&newOrders11[count13], sizeof(struct orders), 1, filePointer))==1)
{
    if(ID == newOrders11[count13].ID) {
        printf("These are the details for order %i\n", count13);

        if(count13>=0)
        printf("Customer ID: %d\n", newOrders11[count13].ID);
        printf("Product Name: %s\n", newOrders11[count13].productname);
        printf("Price: %f\n", newOrders11[count13].total);
    } else {
        continue;
    }
    count13++;
}

Does any one know how I should go about this? Sorry, I am still a C beginner. This is my first program in C.

4

2 回答 2

0

Within your while loop, you're only testing against the customer ID, so the first record for that customer will match.

Given that records in the file are ordered by timestamp, it might be better to start reading from the end of the file, rather than the beginning:

fseek(filePointer, -sizeof (struct orders), SEEK_END);
while (count13 <= MAXORDERSTOBEMADE && (fread(...) == 1))
{
  if (ID == ...)
  {
     ...
  }    
  else
  {
    fseek(filePointer, -2 * sizeof(struct orders), SEEK_CUR);
  }
}

Caveats: SEEK_END may not be supported for binary streams, and arbitrary offsets may not be meaningful for text streams:

7.21.9.2 The fseek function

2 The fseek function sets the file position indicator for the stream pointed to by stream. If a read or write error occurs, the error indicator for the stream is set and fseek fails.

3 For a binary stream, the new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified by whence. The specified position is the beginning of the file if whence is SEEK_SET, the current value of the file position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.

4 For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier successful call to the ftell function on a stream associated with the same file and whence shall be SEEK_SET.
于 2013-01-08T12:29:55.623 回答
0

You need to scan through the whole file until you are sure you will not see any more orders. In other words, when you see EOF (end-of-file), you should print the last match you found.

  • So, read through the entire file, one record at a time.
  • If you find a match, store it temporarily in variables for later printing (overwriting any previous match - you only need to remember the last one found).

(optionally, you may want to compare the timestamps and ignore matches with older timestamps. If your file is written in timestamp order this step can be omitted)

  • After reading through the whole file, print the values of the variables where you stored the latest record.
于 2013-01-08T12:39:08.413 回答