gpt4 book ai didi

c - 如何从c中的文件中读取日期

转载 作者:行者123 更新时间:2023-12-01 13:57:54 24 4
gpt4 key购买 nike

我在一个使用 C 的程序中读取一个文件。在文件中我有一些日期,每个日期都在单独的一行中,就像这样:

20190101
20190304
20180922

现在我希望程序将这些作为日期读取并找出当前日期与这些日期之间的差异。是否可以将这些日期转换为 C 可读的格式?像这样:2019.01.01目前,我使用 fgets 读取它,但无法将其转换为上述格式。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
void FileRead(FILE *pr)
{
char date [15];
fgets(date,15,pr);
printf("date : %s", date);

}
int main()
{
char x;
FILE *pr;
pr=fopen("mytextfile.txt","r");
if(pr==NULL)
{
printf("File can not be opened.");
return 0;
}
while(scanf("%c", &x))
{
switch(x)
{

case 'v' :
FileRead(pr);
break;

case 'k' :
return 0;

}
}
fclose(pr);
return 0;
}

最佳答案

处理从现在到那时的差异的最简单方法是从文件中读取日期并将它们解析为月、日和年 (m, d, y)。如果您不打算对每次转换进行完整验证,那么分隔 4 位数年份和 2 位数月份和日期的简单方法是使用带有适当 字段的 fscanf -width 修饰符以将转换限制为所需的位数,例如

    while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {

然后循环中所需要做的就是用年、月和日值填充 struct tm(记住从年中减去 1900,并设置每个小时、分钟和秒成员都为零,夏令时成员为 -1)。一个简单的函数可以做到这一点,并在调用 mktime 后返回 time_t,例如

time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };

return mktime(&bdt); /* return mktime conversion to time_t */
}

最后,您需要获取 time_t 值并调用 difftime 以获取当前时间与从文件中读取的时间之间的秒差(以秒为单位) double 值。继续 main() 中的循环,例如

    while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
printf ("date[%d] %d/%d/%d is %.2f seconds from now.\n",
n++, m, d, y, difftime (now, then));
}

总而言之,您可以执行以下操作:

#include <stdio.h>
#include <time.h>

time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };

return mktime(&bdt); /* return mktime conversion to time_t */
}

int main (int argc, char **argv) {
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
int y, m, d, n = 1;

if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}

while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
printf ("date[%d] %d/%d/%d is %.2f seconds from now.\n",
n++, m, d, y, difftime (now, then));
}
if (fp != stdin) fclose (fp); /* close file if not stdin */

return 0;
}

(注意:程序希望文件名作为程序的第一个参数读取日期(或者如果没有参数,程序将默认从 stdin 读取给出))

示例输入文件

$ cat dat/3dates.txt
20190101
20190304
20180922

示例使用/输出

$ ./bin/time_from_now dat/3dates.txt
date[1] 1/1/2019 is 6300212.00 seconds from now.
date[2] 3/4/2019 is 943412.00 seconds from now.
date[3] 9/22/2018 is 15030212.00 seconds from now.

编辑每条评论修改输入文件格式

如果您的数据文件实际上与您最初随问题发布的三行日期不同,并且它包含日期信息之前的标题行,那么您需要在处理日期行之前阅读、识别和处理这些行.由于您希望以天而不是秒为单位输出,您只需将秒数除以 86400 即可获得以天为单位的时差。

要读取句柄标题行,只需将您的读取调整为一次将整行读取到足够大小的缓冲区中。声明一个足够大的常量以确保您的缓冲区足够大,例如

#define MAXC 1024u  /* if you need a constant, #define one (or more) */
...
int main (int argc, char **argv) {
...
char buf[MAXC]; /* buffer to hold each line read from file */

然后您将简单地使用 sscanf 而不是 fscanf 来对每一行的信息进行完全相同的解析。如果行格式不满足 yyyymmdd 格式,您知道它不是日期行 - 以任何您想要的方式处理这些行(它们只是在下面的示例中以前缀 "non -日期行:“

结合每天将文件中时间以来的秒数除以 86400 秒,您的新读取循环将类似于:

    while (fgets (buf, MAXC, fp)) {     /* read each line in file */
/* if line isn't a date line, just output line as non-date line */
if (sscanf (buf, "%4d%2d%2d", &y, &m, &d) != 3) {
printf ("non-date line: %s", buf);
continue;
}
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
double secs = difftime (now, then); /* get seconds between dates */
printf ("date[%d] %02d/%02d/%04d is %11.2f sec (%g days) from now.\n",
n++, m, d, y, secs, secs / 86400.0);
}

你说:

"I am not able to open the file"

程序希望您提供要读取的文件名作为程序的第一个参数,否则程序将默认从 stdin 读取。这意味着您必须向程序提供文件名,例如

./yourprogram your_date_file

或者您必须在 stdin 上提供该数据,方法是将该信息从其他程序的输出管道传输到该程序,或者简单地将文件重定向为 stdin 上的输入>,例如

some_utility_making_dates | ./yourprogram

./yourprogram < your_date_file

合并所有更改,您的程序将如下所示:

#include <stdio.h>
#include <time.h>

#define MAXC 1024u /* if you need a constant, #define one (or more) */

time_t fill_broken_down_time (int y, int m, int d)
{ /* initialize struct members */
struct tm bdt = { .tm_sec=0, .tm_min=0, .tm_hour=0, .tm_mday=d,
.tm_mon=m>0?m-1:0, .tm_year=y-1900, .tm_isdst=-1 };

return mktime(&bdt); /* return mktime conversion to time_t */
}

int main (int argc, char **argv) {
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
int y, m, d, n = 1;
char buf[MAXC]; /* buffer to hold each line read from file */

if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}

while (fgets (buf, MAXC, fp)) { /* read each line in file */
/* if line isn't a date line, just output line as non-date line */
if (sscanf (buf, "%4d%2d%2d", &y, &m, &d) != 3) {
printf ("non-date line: %s", buf);
continue;
}
time_t now = time(NULL),
then = fill_broken_down_time (y, m, d);
double secs = difftime (now, then); /* get seconds between dates */
printf ("date[%d] %02d/%02d/%04d is %11.2f sec (%g days) from now.\n",
n++, m, d, y, secs, secs / 86400.0);
}
if (fp != stdin) fclose (fp); /* close file if not stdin */

return 0;
}

带标题的示例输入文件

$ cat dat/3dates-w-headers.txt
This file contains dates to read and convert to days.
The file also contains this description and dates in the format:

yyyymmdd
20190101
20190304
20180922

示例使用/输出

$ ./bin/time_from_now2 dat/3dates-w-headers.txt
non-date line: This file contains dates to read and convert to days.
non-date line: The file also contains this description and dates in the format:
non-date line:
non-date line: yyyymmdd
date[1] 01/01/2019 is 6348645.00 sec (73.4797 days) from now.
date[2] 03/04/2019 is 991845.00 sec (11.4797 days) from now.
date[3] 09/22/2018 is 15078645.00 sec (174.521 days) from now.

检查一下,如果您还有其他问题,请告诉我。

关于c - 如何从c中的文件中读取日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55174868/

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