这个错误自己经常碰到的,不过这次自己一直在看自己的程序,而且还是从书上抄的,应改没错啊。谁知道是因为自己的头文件顺序影响了。
因为自己用的是strptime函数。所以需要明确请求使用X/Open的标准功能。所以应该在头文件前加上#define _XOPEN_SOURCE ,而且是必须在所有的头文件前面。
至于#define _XOPEN_SOURCE的作用,自己也不太明了。搜索如下:
Glibc 所实现全部或部分规范下的功能有: 1.ISO C: C语言国际标准. 2.POSIX: 操作系统的 ISO/IEC 9945 (aka IEEE 1003) 标准. 3.Berkeley Unix: BSD 和 SunOS. 4.SVID: V 系统接口描述. 5.XPG: The X/Open Portability Guide. 程序中,为了实现上述功能需要定义对应的宏。如第二个功能POSIX,应定义 _POSIX_SOURCE。类似的有 _BSD_SOURCE、_SVID_SOURCE、_XOPEN_SOURCE。 也就是说: #define _XOPEN_SOURCE 是为了可以使用 5. The X/Open Portability Guide 的功能。 [实用方法] 使用上述五种功能的方法有两种: 1)在cc命令中指定,如:cc -D _POSIX_SOURCE file.c 2)将源程序的第一行设置为:#define _POSIX_SOURCE 1 [_XOPEN_SOURCE是什么?] 翻译成汉语表达很蹩脚,英文原版解释如下: Macro: _XOPEN_SOURCE If you define this macro, functionality described in the X/Open Portability Guide is included. This is a superset of the POSIX.1 and POSIX.2 functionality and in fact _POSIX_SOURCE and _POSIX_C_SOURCE are automatically defined. As the unification of all Unices, functionality only available in BSD and SVID is also included. If the macro _XOPEN_SOURCE_EXTENDED is also defined, even more functionality is available. The exa functions will make all functions available which are necessary for the X/Open Unix brand.书上代码也贴下了:
#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
struct tm *tm_ptr;
struct tm timestruct; //申请了空间~~~
time_t the_time;
char buf[256];
char *result; //不必有内存空间~~~
(void) time(&the_time);
tm_ptr = localtime(&the_time);
strftime( buf, 256, "%A %d %B, %I:%M %p", tm_ptr );
printf( "strftime gives: %s\n", buf );
strcpy( buf, "Thu 26 July 2007, 17:53 will do fine" );
printf( "calling strptime with: %s\n", buf );
tm_ptr = ×truct;
result = strptime( buf, "%a %d %b %Y, %R", tm_ptr ); //strptime怎么实现的?
printf( "strptime consumed up to: %s\n", result );
printf( "strptime gives: \n" );
printf( "date: %02d/%02d/%02d\n",
tm_ptr->tm_year % 100, tm_ptr->tm_mon+1, tm_ptr->tm_mday );
printf("time: %02d:%02d\n",
tm_ptr->tm_hour, tm_ptr->tm_min );
exit(0);
}