gpt4 book ai didi

c - 从文件转换时间并与当前时间进行比较

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:32 25 4
gpt4 key购买 nike

我之前问过一些类似的问题,但看起来,我从文件中获取的时间并没有转换为您从 localtime() 和 time() 中获取的相同类型的时间。

正是我想要的是:

我有一个包含以下信息的 txt 文件

订单.txt

file1.txt;5;15:40

file2.txt;7;15:41

file1.txt;10;16:00

我的目标是获取 hour:min (15:40) 的时间,然后,如果它与 currentTime 相同,则显示 file1.txt内容。否则,如果 currentTime 低于 hour:min 时间 (15:40),它会等到 15:40 并显示file1.txt 内容。如果 currentTime 更高,则忽略第一个并转到下一个。

例如,如果当前时间15:39,它将等待1分钟然后显示file1 .txt 内容。如果当前时间15:40,则直接显示file1.txt内容,无需等待。如果当前时间15:41,它只是转到下一个(file2.txt)并再次检查。

使用的变量

FILE* orderFile;
FILE* contentFile;
FILE* defaultFile; (This will be used when this code works)
char fileName[50];
char textContent[5000];
int seconds;
int hour, min;
int diff_time;
char timeValue[50];
time_t current;

包括

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

为此,我尝试了以下代码:

orderFile = fopen("order.txt","r");
defaultFile = fopen("default.txt","r");

while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue)
{

sscanf(timeValue,"%d:%d",&hour,&min);

contentFile = fopen(fileName,"r");

if(contentFile != NULL)
{
fseek (contentFile, 0, SEEK_END);
size = ftell(contentFile);
rewind(contentFile);

if(size == 0)
{
printf("\nEmpty file. (Scanned file: %s)\n", fileName);
fclose(contentFile);
}
else
{

time_t now;
time(&now);

struct tm file_time_tm;
file_time_tm = *localtime(&now);
file_time_tm.tm_hour = hour;
file_time_tm.tm_min = min;
file_time_tm.tm_sec = 0;
file_time_tm.tm_isdst = -1;
time_t fileTime = mktime(&file_time_tm);

double diff_time = difftime(fileTime, now);

if(diff_time == 0)
{
while(fscanf(contentFile,"%[^\t]",textContent) == 1)
{
printf("%s\n", textContent);
}
sleep(seconds);
}
else
{
if(diff_time > 0)
{
while(fscanf(defaultFile,"%[^\t]",defaultContent) == 1)
{
printf("%s\n", defaultContent);
}
sleep(diff_time);

while(fscanf(contentFile,"%[^\t]",textContent) == 1)
{
printf("%s\n", textContent);
}
sleep(seconds);
}
}

fclose(defaultFile);
fclose(contentFile);
}
}
else
{
if(contentFile == NULL)
{
printf("\nFile does not exist. (Scanned file: %s)\n", fileName);
fclose(contentFile);
}
}
}

fclose(orderFile);
printf("\n");

感谢 chux,它现在可以很好地工作,但还没有完全工作。

如果我有以下内容:

当前时间:15:00

订单.txt

file1.txt;5;15:01

file2.txt;6;15:02

file3.txt;3;15:03

当我运行程序时,会发生以下情况:

DEFAULT MESSAGE (text inside default.txt)

WAITS DIFF_TIME (CORRECTLY DONE)

SHOWS CONTENT FROM THE file1.txt

WAITS 5 SECONDS

X - SHOWS CONTENT FROM THE file2.txt (This is wrong, it should check again and if the time is 15:02 it will show, not 15:01. Which I assume it's because it is still reading as 15:01 on the timeValue and not 15:02, any idea why?)

WAITS UNTILL IT'S 15:02 (wrong)

SHOWS file3.txt (wrong)

WAITS UNTILL IT'S 15:03

ENDS (wrong as it should not end after waiting, it should end when file3.txt shows and waits for 3 seconds)

我需要的输出:

DEFAULT MESSAGE

waits diff_time

file1.txt content

waits 5 seconds

DEFAULT MESSAGE

waits diff_time

file2.txt content

waits 7 seconds

DEFAULT MESSAGE

waits diff_time

file3.txt content

waits 3 seconds

ENDS

如果我同时有两个文件,它也不能正常工作,例如:

订单.txt

file1.txt;5;15:01

file2.txt;6;15:01

file3.txt;3;15:02

它也打破了它。知道如何正确执行此操作吗?

最佳答案

正如@M Oehm 上面的评论,很明显编译器警告没有完全启用。建议启用它们 - 这会节省您的时间。

问题在于时间的比较和减法。

// Troublesome code
if(currentTime->tm_hour < hour && currentTime->tm_min < min) {
diff_time = difftime(currentTime,timeValue);
printf("%d - Or this?\n", diff_time);
sleep(diff_time);
}
  1. 比较好奇。通常这样的比较会是这样的形式

    if (h1 < h2 || (h1 == h2 && m1 < m2))
  2. difftime 需要 2 个 time_t 参数,而不是 char timeValue[50] 和一个 struct tm .

  3. 传递给 double difftime(time_t time1, time_t time0) 的值向后显示。结果是 time1 - time0

改为推荐

    int current_hm = currentTime->tm_hour * 60 + currentTime->tm_min;
int file_hm = hour * 60 + min;

if (current_hm < file_hm) {
unsigned diff_time = (file_hm - current_hm)*60u;
printf("%u - Or this?\n", diff_time);
sleep(diff_time);
}

[编辑]简化方法。

    time_t now;
time(&now);

struct tm file_time_tm;
file_time_tm = *localtime(&now);
file_time_tm.tm_hour = hour; // from file
file_time_tm.tm_min = min; // from file
file_time_tm.tm_sec = 0;
file_time_tm.tm_isdst = -1;
time_t filetime = mktime(&file_time_tm);

double diff = difftime(filetime, now); // filetime - now
if (diff > 0) {
sleep((unsigned) diff);
}

关于c - 从文件转换时间并与当前时间进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27756571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com