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.
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 Thefseek
function sets the file position indicator for the stream pointed to bystream
. If a read or write error occurs, the error indicator for the stream is set andfseek
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 bywhence
. The specified position is the beginning of the file if whence isSEEK_SET
, the current value of the file position indicator ifSEEK_CUR
, or end-of-file ifSEEK_END
. A binary stream need not meaningfully supportfseek
calls with awhence
value ofSEEK_END
.
4 For a text stream, eitheroffset
shall be zero, oroffset
shall be a value returned by an earlier successful call to theftell
function on a stream associated with the same file andwhence
shall beSEEK_SET
.
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.
(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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.