Trouble compiling a program which should write a new file every day

Hello there,

I have again trouble compiling my program file. The program should be started manually and then run continously until someone stops it manually.
First, what it should do:
In this time it should read out values from an external source once a day (at midnight possibly) it should close the file in the format file_date.dat and open a new file and so on.

I went to a friend of mine who studies IT and he wrote me something, but it fails compiling and he is in holidays for two weeks^^

Here is the error message:
"
/usr/include/time.h:199: error: too few arguments to function `size_t
strftime(char*, unsigned int, const char*, const tm*)'
"
There are other errors but I think they all point to the same error.

Here is the code my friend implemented:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string dayWhenFileWasLastWritten = "";

	while ((timediff < m_time) || m_time == 0)

	{

		if (strftime("%d") != dayWhenFileWasLastWritten) {
			errorcode = CAENVME_SetOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 1 in order to prevent from any events being written to the QDC and the GPS Card.

			if (rawfile != null && rawfile.is_open()) {
				rawfile.close();
			}
			ofstream rawfile("rawfile" + strftime("%Y-%m-%d") + ".dat");
			dayWhenFileWasLastWritten = strftime("%d");

			errorcode1 = CAENVME_ReadCycle(Handle, clearreg, &data, cvA24_U_DATA, cvD16);
			mbg_clr_ucap_buff(dh);
			errorcode = CAENVME_ClearOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 0 in order to let events come through to the GPS Card and the QDC
		}


maybe you guys can help me.
Thank you
You give to few arguments to strftime it needs 4 arguments not 1.

http://www.cplusplus.com/reference/clibrary/ctime/strftime/

ln 14 would have to be comething like:
 
strftime (dayWhenFileWasLastWritten,80,"%d",localtime ( &rawtime ));

(I didn't test this, it's a copy/past from your code and the example code)
Last edited on
Hi, I just tried your correction but I still get the same error message.
It can't be the same error. You need to be clear on what error the compiler is reporting.

Pass in dayWhenFileWasLastWritten.c_str() as the first parameter.
http://lmgtfy.com/?q=man+strftime
Hi,

I meant the error with the time.h file and the too few arguments I posted in my first post.

I am sure you are laughing hard now but when you guys say things like "Pass in dayWhenFileWasLastWritten.c_str() as the first parameter." I don't really get what you want me to do^^.

I tried looking strftime up on google and here and I found something, it just doesn't really help me. That must be how my mother feels when I tell her to navigate to the control Panel and change the resolution :)

So it would be really nice if you would be a bit indulging with me and treat me more like an idiot who does not know what he is doing but needs to get this program running of which this here is the last step (erase that, put this in instead. something like that)

Thanks
Your reported error is:
/usr/include/time.h:199: error: too few arguments to function `size_t strftime(char*, unsigned int, const char*, const tm*)'

Thenero asked you to change your line:
strftime("%d");
to:
strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime));

If you made that change, you can't possibly get the same error.

I've send you the means to find out what are the correct arguments to strftime with:
http://lmgtfy.com/?q=man+strftime

And you need to change the declaration:
string dayWhenFileWasLastWritten = "";
to
char dayWhenFileWasLastWritten[80];
Last edited on
Thank you,

Here is my new code:
char dayWhenFileWasLastWritten[80];

	while ((timediff < m_time) || m_time == 0)

	{

		if (strftime("%d") != dayWhenFileWasLastWritten) {
		//if (strftime("%d") != dayWhenFileWasLastWritten.c_str()) {
			errorcode = CAENVME_SetOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 1 in order to prevent from any events being written to the QDC and the GPS Card.

			if (rawfile != null && rawfile.is_open()) {
				rawfile.close();
			}
			ofstream rawfile("rawfile" + strftime("%Y-%m-%d") + ".dat");
			//dayWhenFileWasLastWritten = strftime("%d");
			
			//strftime (dayWhenFileWasLastWritten,80,"%d",localtime ( &rawtime ));
			strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime));
			
			errorcode1 = CAENVME_ReadCycle(Handle, clearreg, &data, cvA24_U_DATA, cvD16);
			mbg_clr_ucap_buff(dh);
			errorcode = CAENVME_ClearOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 0 in order to let events come through to the GPS Card and the QDC
		}


And this is the error (the first one at least) I get when compiling:
/usr/include/time.h:199: error: too few arguments to function `size_t
strftime(char*, unsigned int, const char*, const tm*)'


So it seems the error stays...
I can post the rest of the error codes, but I think one piece at the time would be better.
And you changed is where?
Huh?

This is my changed code.
I changed line 14 in the original to strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime)); and line 1 to char dayWhenFileWasLastWritten[80];

Wasn't that what I was supposed to do?
I see. What about the other one. Have you looked that the line that the compiler indicated the error was on?
Just pay attention to what the compiler is telling you. Function strftime() takes four arguments. You're passing just one argument in at least four cases.
You mean the line 199 in the time.h file?

In the time.h file it says:
1
2
3
4
5
6
7
/* Format TP into S according to FORMAT.
   Write no more than MAXSIZE characters and return the number
   of characters written, or 0 if it would exceed MAXSIZE.  */
extern size_t strftime (char *__restrict __s, size_t __maxsize,
			__const char *__restrict __format,
			__const struct tm *__restrict __tp) __THROW;
__END_NAMESPACE_STD


Line 199 from my error message corresponds to line __const struct tm *__restrict __tp) __THROW;

Did you mean this line kbw?
was that not it? Do you need more information? Or is all the info here and I don't see it?

Thank you
Ok, now I got it (with help though)

I changed the code, the "too few arguments" error is gone, my code looks now like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//string dayWhenFileWasLastWritten = "";
	char dayWhenFileWasLastWritten[80];

	while ((timediff < m_time) || m_time == 0)

	{
		time_t rawtime;
		if (strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime)) != dayWhenFileWasLastWritten) {
		//if (strftime("%d") != dayWhenFileWasLastWritten.c_str()) {
			errorcode = CAENVME_SetOutputRegister(Handle, Mask);

			if (rawfile != null && rawfile.is_open()) {
				rawfile.close();
			}
			ofstream rawfile("rawfile" + strftime(dayWhenFileWasLastWritten,80,"%Y-%m-%d",localtime (&rawtime)) + ".dat");
			//dayWhenFileWasLastWritten = strftime("%d");
			
			//strftime (dayWhenFileWasLastWritten,80,"%d",localtime ( &rawtime ));
			strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime));
			
			errorcode1 = CAENVME_ReadCycle(Handle, clearreg, &data, cvA24_U_DATA, cvD16);
			mbg_clr_ucap_buff(dh);
			errorcode = CAENVME_ClearOutputRegister(Handle, Mask); 

		}


with Line 214 corresponding to if (strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime)) != dayWhenFileWasLastWritten) {
I get a different error message:

1
2
3
4
5
6
7
8
9
10
v265_j.cc:214: error: ISO C++ forbids comparison between pointer and integer
v265_j.cc:218: error: `null' undeclared (first use this function)
v265_j.cc:218: error: (Each undeclared identifier is reported only once for 
   each function it appears in.)
v265_j.cc:218: error: request for member `is_open' in `rawfile', which is of 
   non-aggregate type `std::ofstream ()()'
v265_j.cc:219: error: request for member `close' in `rawfile', which is of 
   non-aggregate type `std::ofstream ()()'
v265_j.cc:221: error: invalid operands of types `const char*' and `const 
   char[5]' to binary `operator+'


Last edited on
The compiler is clear. You can't compare a pointer and an integer. dayWhenFileWasLastWritten is a pointer and strftime returns an integral type (size_t).

I just don't understand why you're calling strftime so many times, just to get the return value.

Your calls to localtime will return giberish because you've not initialised rawtime.
I just don't understand why you're calling strftime so many times, just to get the return value.

I don't mind doing the task it should do completely different. I just need it doing that^^

The compiler is clear. You can't compare a pointer and an integer. dayWhenFileWasLastWritten is a pointer and strftime returns an integral type (size_t).

I understand that you want to help me but you have to forgive me: I am not planning on learning how to program, I just need this working. I should have gone to a professional in the first place maybe but now I am smarter...

Anyway if anybody is interested, you can pm me, we could meet up with Skype or something and you could help me. I would pay for that of course via Paypal or something.
Last edited on
I worked and sweated very hard and it compiles and runs :)

Here is the corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	

        time_t rawtime;
	time (&rawtime);
	char dayWhenFileWasLastWritten[80] = "0";
	char currentday[80] = "0";
	char currentdate[80];
	strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime));

    ofstream rawfile(currentdate);	
















    while ((timediff < m_time) || m_time == 0)

    {






		cout << "test2...\n";
		
		strftime(currentday,80,"%d",localtime (&rawtime));
		strftime(currentdate,80,"%Y-%m-%d",localtime (&rawtime));
		
		cout << "test3...\n";


		if ( currentday != dayWhenFileWasLastWritten) {
			cout << "test4...\n";
		
		

		//if (strftime("%d") != dayWhenFileWasLastWritten.c_str()) {
			errorcode = CAENVME_SetOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 1 in order to prevent from any events being written to the QDC and the GPS Card.

			if (rawfile != NULL && rawfile.is_open()) {
				rawfile.close();
			}
			
			
			//string completename = prefix+suffix;
			
			//ofstream rawfile("rawfile" + currentday + ".dat");
			ofstream rawfile(currentdate".dat");
			//dayWhenFileWasLastWritten = strftime("%d");
			
			//strftime (dayWhenFileWasLastWritten,80,"%d",localtime ( &rawtime ));
			strftime(dayWhenFileWasLastWritten,80,"%d",localtime (&rawtime));
			
			errorcode1 = CAENVME_ReadCycle(Handle, clearreg, &data, cvA24_U_DATA, cvD16);
			mbg_clr_ucap_buff(dh);
			errorcode = CAENVME_ClearOutputRegister(Handle, Mask); // Sets the NIM Level of channel0 output to a logical 0 in order to let events come through to the GPS Card and the QDC
			errorcode1 = CAENVME_ReadCycle(Handle, statusreg, &data,cvA24_U_DATA, cvD16); 
	    
			READY = (data & 0x8000)/32678;
			while (READY ==0)
			   { 
				//read status
				errorcode1 = CAENVME_ReadCycle(Handle, statusreg,&data,cvA24_U_DATA, cvD16); 
			READY = (data & 0x8000)/32678;
  			}
		}





The remaining problem is, that the comparison if ( currentday != dayWhenFileWasLastWritten) is true every time though it should only be one time a day. Can anybody see my mistake?

Thank you
Replace:
if (currentday != dayWhenFileWasLastWritten)
with:
if (strcmp(currentday, dayWhenFileWasLastWritten) != 0)
Topic archived. No new replies allowed.