我正在树莓派上写一个c++程序。我使用ctime库来获取当前时间和日期,使其成为文本文件的标题。例如,我所在的当前日期和时间是2015年10月23日14:51。因此,文本文件的名称将是20151023_14_51.txt。下面是代码:
FILE *f;
main(int argc, char *argv[]){
char dateiname[256]="";
time_t t = time(0);
struct tm * now = localtime(&t);
//Create and open file
sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt",
now->tm_year+1900,
now->tm_mon+1,
now->tm_mday,
now->tm_hour,
now->tm_min;
f = fopen(dateiname, "w");
我的问题是,当我试图用gcc编译程序时,我得到以下性质的错误:
错误:无效使用不完整类型'struct main(int, char**)::tm'
错误:struct main(int, char**)::tm
我也得到这个错误在开始:
错误:'localtime'未在此范围内声明
我做了一些研究,发现有类似问题的人没有包括sys/time.h,但我包括了它。以下是我包含的内容:
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
有没有人知道是什么可能导致这些错误,或者如果我错过了什么?谢谢。
struct tm
由#include <time.h>
定义,或者由#include <ctime>
定义,并使用std::tm
作为名称。