這是一個根據用戶輸入的時間值生成事件的函式,然后將值轉換為 Unix Epochmktime并將它們存盤到檔案中。我已經測驗并發現其他一切都運行良好,直到我們到達用戶輸入值的部分似乎我沒有使用指向井的指標event,struct tm所以我需要幫助才能正確完成。
int EventCreator(){
FILE *fptr = fopen("Events_Calendar.txt", "a ");
struct tm *event;
printf("Enter the values of the hour minute day month year in digits and this specific format meaning using spaces to seperate them");
printf("\n hr min day mon yr\n>");
scanf("%d %d %d %d %d", event->tm_hour, event->tm_min, event->tm_mday, event->tm_mon, event->tm_year);
time_t NineteenEvent = mktime(event);
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
uj5u.com熱心網友回復:
您已宣告指向 a 的指標,struct tm但尚未為 a 分配記憶體struct tm。我建議您event改為創建一個自動變數。
例子:
void EventCreator() { // make the function `void` since you do not return anything
FILE *fptr = fopen("Events_Calendar.txt", "a ");
if (fptr) { // check that opening the file succeeds
// `event` is now not a pointer and set `tm_isdst` to a negative value
// to make `mktime` guess if DST is in effect:
struct tm event = {.tm_isdst = -1};
printf(
"Enter the values of the hour minute day month year in digits and "
"this specific format meaning using spaces to seperate them\n"
"hr min day mon yr\n>");
// %d requires an `int*` but you've supplied it with `int`.
// (also, check that scanf succeeds)
if (scanf("%d %d %d %d %d", &event.tm_hour, &event.tm_min,
&event.tm_mday, &event.tm_mon, &event.tm_year) == 5) {
event.tm_year -= 1900; // years since 1900
--event.tm_mon; // 0-11
time_t NineteenEvent = mktime(&event); // take its address here
printf("%ld", NineteenEvent);
fprintf(fptr, "%ld\n", NineteenEvent);
fclose(fptr);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/412257.html
標籤:
下一篇:如何呼叫指標函式
