我正在使用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;
}
答案 0 :(得分:1)
一旦您通过char*
拥有fgets
,请使用atoi
转换为整数。然后,您将获得YYYYMMDD格式的日期。然后,您可以
const int day = dateYYYYMMDD % 100;
const int month = (dateYYYYMMDD % 10000)/100;
const int year = dateYYYYMMDD / 10000;
您可以使用C Date and time functions来获取当前日期。
要使当前日期与这些日期之间有所不同,一种选择是将所有日期转换为Julian date,然后对朱利安日期进行数字差异。
如果很难获取居利安日期,请归一化为同一年(从低年级到高年级,基于年份添加365或366是否飞跃)。然后到同一个月,然后进行日差。
答案 1 :(得分:0)
输出是,您只需循环文件行并在其中设置if条件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main(void)
{
char buff[20];
time_t now = time(NULL);
strftime(buff, 20, "%Y%m%d\n", localtime(&now));
printf("Today : %s \n\n", buff);
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if (file != NULL)
{
char line[128]; /* or other suitable maximum line size */
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{
fputs(line, stdout); /* write the line */
if (strcmp(line, buff) == 0)
printf("\nThis line of date equal to current date\n");
}
fclose(file);
}
else
{
perror(filename); /* why didn't the file open? */
}
return 0;
}
file.txt
20190315
20190304
20180922
答案 2 :(得分:0)
从现在到现在,解决差异的最简单方法是从文件中读取日期并将其解析为月,日和年(m, d, y
)。如果您不打算对每次转换进行完整的验证,则分隔4位数字和2位数字的月份和日期的简单方法是使用fscanf
和适当的 field-width < / em>修饰符可将转换限制为所需的位数,例如
while (fscanf (fp, "%4d%2d%2d", &y, &m, &d) == 3) {
那么循环中所需要做的就是用年,月和日的值填充struct tm
(记住从年中减去1900
,并设置小时,分钟和第二个成员为零,夏令时成员为-1
)。一个简单的函数可以执行此操作,并在调用time_t
之后返回mktime
,例如
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-date line: "
输出。>
结合将文件中的时间以来的秒数除以每天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.
仔细检查一下,如果还有其他问题,请告诉我。