C 从二进制文件读取和显示时间戳

C 从二进制文件读取和显示时间戳,c,struct,time-t,C,Struct,Time T,我目前正在从事一个大学c项目,在该项目中,我将保存在结构中的读数写入二进制文件。在此结构中,我还保存了输入项目的时间和日期。然后,我在一个单独的程序中打开文件并读取值,这很好,但是我很难理解如何读取并在屏幕上显示文件中保存的时间和日期。下面是我的第二个课程中该部分的代码 #include <stdio.h> #include <stdlib.h> #include <time.h> #pragma warning(disable : 4996) struct

我目前正在从事一个大学c项目,在该项目中,我将保存在结构中的读数写入二进制文件。在此结构中,我还保存了输入项目的时间和日期。然后,我在一个单独的程序中打开文件并读取值,这很好,但是我很难理解如何读取并在屏幕上显示文件中保存的时间和日期。下面是我的第二个课程中该部分的代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#pragma warning(disable : 4996)

struct readings {
    double temperature;
    double wind_direction;
    double wind_speed;
    time_t time;
};



int main(void)
{
    struct readings* readings = NULL;
    int option;
    printf_s("Welcome to Data Acquisition Program\n");
    printf_s("Options are as follows:\n 1:Load File\n 2:Search Weather by date\n 3:View Monthly Data\n 4:Export to Excel\n 5:Exit\n");
    printf_s("Enter an option: ");
    scanf_s("%d", &option);

    if (option == 1)
    {

        FILE* pFile;
        errno_t error;
        int num_readings;
        //struct readings* time_t time;
        time_t t;
        struct tm* tmp;
        char MY_TIME[50];
        time(&t);

        tmp = localtime(&t);

        error = fopen_s(&pFile, "weather.bin", "rb");

        if (error == 0)
        {
            fread(&num_readings, sizeof(int), 1, pFile);
            readings = malloc(sizeof(struct readings) * num_readings);
            fread(readings, sizeof(struct readings), num_readings, pFile);

            strftime(MY_TIME, sizeof(MY_TIME), "%x - %I:%M%p", tmp);
            printf("Date & Time: %s\n", MY_TIME);

            for (int i = 0; i < num_readings; i++)
            {
                printf_s("Temperature: %lf\n", readings[i].temperature);
                printf_s("Wind Direction: %lf\n", readings[i].wind_direction);
                printf_s("Wind Speed: %lf\n", readings[i].wind_speed);
            }
            fclose(pFile);
        }
        else { printf_s("Error: %d", error); }
    }
#包括
#包括
#包括
#杂注警告(禁用:4996)
结构读数{
双温;
双风向;
双风速;
时间;
};
内部主(空)
{
结构读数*读数=NULL;
int选项;
printf_s(“欢迎参加数据采集计划”);
printf_s(“选项如下:\n 1:加载文件\n 2:按日期搜索天气\n 3:查看月度数据\n 4:导出到Excel\n 5:退出\n”);
printf_s(“输入选项:”);
扫描单位(“%d”和选项);
如果(选项==1)
{
文件*pFile;
错误没有错误;
int num_读数;
//结构读数*时间t时间;
时间;
struct tm*tmp;
查我的时间[50];
时间&t;
tmp=本地时间(&t);
错误=fopen_s(&pFile,“weather.bin”,“rb”);
如果(错误==0)
{
fread(&num_读数,sizeof(int),1,pFile);
读数=malloc(sizeof(结构读数)*num_读数);
fread(读数、sizeof(结构读数)、num_读数、pFile);
strftime(我的时间,sizeof(我的时间),%x-%I:%M%p”,tmp);
printf(“日期和时间:%s\n”,我的时间);
对于(int i=0;i
我想你在找

ctime(&(readings[i].time))
但请不要用那个符号,而是用它

for (struct readings *t = readings; t < readings + num_readings; t++) {
        printf_s("Temperature: %lf\n", t->temperature);
        printf_s("Wind Direction: %lf\n", t->wind_direction);
        printf_s("Wind Speed: %lf\n", t->wind_speed);
        printf_s("Time: %s\n", ctime(&t->time));
}  
for(结构读数*t=读数;t温度);
打印(“风向:%lf\n”,t->风向);
打印(“风速:%lf\n”,t->风速);
打印时间(“时间:%s\n”,ctime(&t->Time));
}  

您能更具体地说明问题的性质吗?请注意,编写结构是有风险的。您需要一个更健壮的序列化机制,因为结构大小可能在不同的平台上发生变化。由于您在同一台主机上写和读,目前这不太可能是一个问题,但需要注意属于