Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程字符串_C_Homework - Fatal编程技术网

C编程字符串

C编程字符串,c,homework,C,Homework,如何从字符串“DDMMYYHHMMSS”中提取前两个字节并填充以下结构 mytime.tm_sec = SS; // Seconds (0-59) mytime.tm_min = MM; // Minutes (0-59) mytime.tm_hour = HH; // Hours (0-23) mytime.tm_mday = YY; // Day of Month (1-31) mytime.tm_mon = MM; // Mont

如何从字符串“DDMMYYHHMMSS”中提取前两个字节并填充以下结构

mytime.tm_sec  = SS;   // Seconds      (0-59)
mytime.tm_min  = MM;   // Minutes      (0-59)
mytime.tm_hour = HH;   // Hours        (0-23)
mytime.tm_mday = YY;   // Day of Month (1-31)
mytime.tm_mon  = MM;   // Month        (0-11)
mytime.tm_year = DD;   // Year         (no. of years since 1900)

也许我不知道;我不理解这个问题,但为什么你不能做以下事情:

assert(strlen(timestring) == 12);

char ss[3];
strncpy(ss, &timestring[10], 2);
mytime.tm_sec = atoi(ss);

// same for others

UPD:不,马上停下来。我忘了
strtime
(查看手册页)。在C语言中,一个字符是一个字节,字符串是字符的数组。您需要在索引0和1处获取
char
s。然后,如果需要,您可以将值转换为
int
s。

我假设是这样,所以我不会给您完整的答案

让我们把这个问题分成几部分

1) 给定一个字符串,您知道如何提取子字符串吗?例如,你能想出如何做到这一点:

char sample[] = "DDMMYYHHMMSS";
char *dd;
// code you need to figure out
printf("%s", dd); // should print “DD”
char dd[] = "32";
int mday;
// code you need to figure out
printf("%d", mday); // should print “32”
提示:查找
strncpy()

2) 一旦你明白了这一点,你知道如何将一串数字转换成一个数字吗?例如,你能做到这一点吗:

char sample[] = "DDMMYYHHMMSS";
char *dd;
// code you need to figure out
printf("%s", dd); // should print “DD”
char dd[] = "32";
int mday;
// code you need to figure out
printf("%d", mday); // should print “32”
提示:查找
sscanf()
&
atoi()


在这一点上,将这两种技术结合起来应该很简单。如果您需要更多帮助,请务必询问更多。

前两个字节?这意味着只有
DD
。这真的是你想要的吗?如果是这样,您是否希望假定当前月份、年份等。?您可以使用
localtime
进行初始化,然后使用要更改的部分进行重写。@stanigator no不是。我首先要真诚地尝试自己解决问题。2.询问现有实施中的具体问题。3.承认问题是家庭作业。4.千万不要使用你不懂的代码。@Mikel不仅要提取连续值,还要提取连续值。你对@maverik的建议有什么具体问题?也许这是一个更好的问题。