3

I am writing a C++ program on the Raspberry Pi. I am using the ctime library to get the current time and date to make it the title of a text file. For example, where I am the current date and time is 14:51 on the 23rd October 2015. So the name of the text file will be 20151023_14_51.txt. Here is the code:

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");

My problem is that when I try to compile the program with gcc I am getting errors of the following nature:

error: invalid use of incomplete type 'struct main(int, char**)::tm'

error: forward decleration of 'struct main(int, char**)::tm'

I also get this error at the beginning:

error: 'localtime' was not declared in this scope

I did some research and found that people with similar problems weren't including sys/time.h but I have that included. Here is what I include:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>

Does anyone have any idea of what could be causing these errors or if I am missing anything? Thanks.

12
  • 1
    You forgot #include <time.h>. Put that in and report back. Try to avoid including sys/ includes directly Commented Oct 23, 2015 at 4:46
  • ok so should i have both, or just the time.h? Commented Oct 23, 2015 at 4:48
  • 1
    C++ does not support implicit int. You need to write int main() or auto main() -> int. Commented Oct 23, 2015 at 4:49
  • @M.M yes that was the problem, thankyou! Commented Oct 23, 2015 at 4:49
  • @Cheersandhth.-Alf yeah I just forgot to write it, thanks though Commented Oct 23, 2015 at 4:50

1 Answer 1

3

The struct tm is defined by either #include <time.h>, or by #include <ctime> and using std::tm for the name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.