从二进制文件中读取和显示时间戳
Reading and displaying time stamp from a binary file
我目前正在从事一个大学 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); }
}
我想你在找
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));
}
我目前正在从事一个大学 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); }
}
我想你在找
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));
}