在 C 中使用 strtok 将日期字符串转换为整数

发布于 2024-12-11 02:47:48 字数 859 浏览 1 评论 0原文

我在使用 strtok() 函数时遇到问题。我输入的日期为 01/01/2000;我的 预期产出为:2000 年 1 月 1 日;但我只得到 1, 1, 1。 这是为什么?

#include <stdio.h>
#include <stdlib.h>
#include "date.h"
#include <string.h>

struct date{
int day;
int month;
int year;
};

Date *date_create(char *datestr){

printf("inside date_create");
char delim[] = "/";
Date* pointerToDateStructure = malloc(sizeof(Date));
 printf("%s",datestr);
char string[10];
*strcpy(string, datestr);
pointerToDateStructure->day = atoi(strtok( string, delim));
pointerToDateStructure->month = atoi(strtok( string, delim));
pointerToDateStructure->year = atoi(strtok( string, delim));
printf("%d", pointerToDateStructure->day);
printf("%d", pointerToDateStructure->month);
printf("%d", pointerToDateStructure->year);

return pointerToDateStructure;
}

I'm having trouble using the strtok() function. I feed this a date of 01/01/2000; my
expected output is: 1, 1, 2000; however I'm just getting 1, 1, 1.
Why is that?

#include <stdio.h>
#include <stdlib.h>
#include "date.h"
#include <string.h>

struct date{
int day;
int month;
int year;
};

Date *date_create(char *datestr){

printf("inside date_create");
char delim[] = "/";
Date* pointerToDateStructure = malloc(sizeof(Date));
 printf("%s",datestr);
char string[10];
*strcpy(string, datestr);
pointerToDateStructure->day = atoi(strtok( string, delim));
pointerToDateStructure->month = atoi(strtok( string, delim));
pointerToDateStructure->year = atoi(strtok( string, delim));
printf("%d", pointerToDateStructure->day);
printf("%d", pointerToDateStructure->month);
printf("%d", pointerToDateStructure->year);

return pointerToDateStructure;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2024-12-18 02:47:48

首先,您要使用strtol而不是atoi(或sscanf,见下文)。函数atoi 是不安全的。

其次,strtok 需要 NULL 而不是 string

pointerToDateStructure->day = atoi(strtok( string, delim));
pointerToDateStructure->month = atoi(strtok( NULL, delim)); /* NULL instead of string. */
pointerToDateStructure->year = atoi(strtok( NULL, delim)); /* See above. */

第三,您没有检查 strtok 返回的值。

附带说明一下,您确定 sscanf 无法解析您的数据吗?

sscanf(str, "%d/%d/%d", &day, &month, &year)

编辑 abelenky 的解释:

函数 strtok 有状态。它“记住”它之前正在处理的字符串,如果您传递“NULL”,它会继续处理同一个字符串,从之前停止的地方开始。如果每次传递一个字符串参数,则每次都会从头开始。

First of all, you want to use strtol instead of atoi (or sscanf, see below). The function atoi is unsafe.

Second, strtok needs NULL instead of string:

pointerToDateStructure->day = atoi(strtok( string, delim));
pointerToDateStructure->month = atoi(strtok( NULL, delim)); /* NULL instead of string. */
pointerToDateStructure->year = atoi(strtok( NULL, delim)); /* See above. */

Third, you are not checking the value returned by strtok.

As a side note, are you sure sscanf can't parse your data ?

sscanf(str, "%d/%d/%d", &day, &month, &year)

EDIT Explanation by abelenky:

The function strtok has state. It "remembers" what string it was working on before, and if you pass "NULL", it continues to work on that same string, picking up where it stopped before. If you pass it a string parameter each time, it starts at the beginning each time.

紫罗兰の梦幻 2024-12-18 02:47:48

sscanf(str, "%02d/%02d/%[^\n], &day, &month, &year) 是最简单的选项之一,但你应该精确与格式;否则一切都会出错。

如果您确实想使用 strtok(),请按照 cnicutar 所说的方式使用它,但每一步都要进行精确的验证。

sscanf(str, "%02d/%02d/%[^\n], &day, &month, &year) is the one of the easiest options, but you should be precise with the format; otherwise everything will go wrong.

If you really want to use strtok(), use it in the exact way as cnicutar said but be precise with verification at each step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文