write time to file

C

childtobe

I want to write the time to a file. (to implement this in an other
piece of code that i've writen)
For some reason he gives me a C2064 error when i try to put the time
into a string.
As you can see in the code i CAN write it to the screen, but not to a
string...
Any ideas?
(I'm not an expert in c++ so please keep it as simple as possible :p)

thanks,

CODE:

#include "stdafx.h"
#include <time.h>
#include <Windows.h>
#include <stdio.h>
#include <string>

char main()
{

SYSTEMTIME st;
GetSystemTime(&st);
char _Datum [11];
char _Tijd[6];

printf("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);
printf("%d:%d\n" ,st.wHour,st.wMinute);

_Datum("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);
// error c2064: term does not evaluate to a
function using 4 arguments

_Tijd("%d:%d\n" ,st.wHour,st.wMinute);
// error c2064: term does not evaluate to a function
using 4 arguments


FILE *fp = fopen("test.txt","a");
fwrite(_Datum,1,11,fp);
fwrite(_Tijd,1,6,fp);
fclose(fp);
return 0;
}
 
P

pasalic.zaharije

(e-mail address removed) je napisao:
I want to write the time to a file. (to implement this in an other
piece of code that i've writen)
For some reason he gives me a C2064 error when i try to put the time
into a string.
As you can see in the code i CAN write it to the screen, but not to a
string...
Any ideas?
(I'm not an expert in c++ so please keep it as simple as possible :p)

thanks,

CODE:

#include "stdafx.h"
#include <time.h>
#include <Windows.h>
#include <stdio.h>
#include <string>

char main()
{

SYSTEMTIME st;
GetSystemTime(&st);
char _Datum [11];
char _Tijd[6];

printf("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);
printf("%d:%d\n" ,st.wHour,st.wMinute);

_Datum("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);
// error c2064: term does not evaluate to a
function using 4 arguments

_Tijd("%d:%d\n" ,st.wHour,st.wMinute);
// error c2064: term does not evaluate to a function
using 4 arguments


FILE *fp = fopen("test.txt","a");
fwrite(_Datum,1,11,fp);
fwrite(_Tijd,1,6,fp);
fclose(fp);
return 0;
}

_Datum is char array, not some object. This line:

_Datum("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);

has no meaning, and it is error. If you wanna to fromat string use
sprintf

sprintf(_Datum, "%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);

Second error is same.

Note that upper code is more C than C++. Some tips:
- do not use printf (or printf-like functions, like sprintf). Use
std::cin
- char array is evil, use std::string
- do not use fopen/fwrite/fputs. Use stream classes for file access
- and even if you use char array, do not limit date with 11 chars

best,
Zaharije Pasalic
 
D

Default User

I want to write the time to a file. (to implement this in an other
piece of code that i've writen)
For some reason he gives me a C2064 error when i try to put the time
into a string.
As you can see in the code i CAN write it to the screen, but not to a
string...
Any ideas?
(I'm not an expert in c++ so please keep it as simple as possible :p)

thanks,
#include "stdafx.h"

The above header is non-standard and not needed.
#include <time.h>
#include <Windows.h>

The above header is non-standard.
#include <stdio.h>
#include <string>

You don't the above header.
char main()

That is not a legal definition of main(). That function must return
int. Always.
{

SYSTEMTIME st;
GetSystemTime(&st);

The above are non-standard. There are perfectly standard ways to get
the system time. I won't comment on them.
char _Datum [11];

All identifiers consisting of an underscore followed by an uppercase
letter are reserved to the implementation. Don't use them. Besides
that, they really don't look good.

char _Tijd[6];

printf("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);
printf("%d:%d\n" ,st.wHour,st.wMinute);

_Datum("%d-%d-%d " ,st.wYear,st.wMonth,st.wDay);

What did you think the above would do? _Datum is a pointer to
character, not a function. Presumably you wanted to set the string to
the text similar to the printf(). sprintf() would write past the end of
the array, as you only allocated 11 characters, so there's no room for
the null-terminator.
// error c2064: term does not evaluate to a
function using 4 arguments

_Tijd("%d:%d\n" ,st.wHour,st.wMinute);

Same thing here.
// error c2064: term does not evaluate to a function
using 4 arguments


FILE *fp = fopen("test.txt","a");

You failed to check whether fopen() succeeded.
fwrite(_Datum,1,11,fp);

A far easier way to do this would have been to skip the char buffers
and use fprintf().
fwrite(_Tijd,1,6,fp);
fclose(fp);
return 0;
}

What you have basically looks like C code. While perfectly legal,
usually C++ programs will use iostream and std::string for these
solutions.

In general, it really doesn't look like you know the language at all.
It seems like you cut and paste code from who knows where without any
real understanding. I don't say that to be mean, but to advise that C++
is a complex language. You need to get a good book (see the FAQ for
recommendations) and work through the basics.




Brian
 
J

Jerry Coffin

I want to write the time to a file. (to implement this in an other
piece of code that i've writen)
For some reason he gives me a C2064 error when i try to put the time
into a string.
As you can see in the code i CAN write it to the screen, but not to a
string...
Any ideas?
(I'm not an expert in c++ so please keep it as simple as possible :p)

// get the current time
time_t now = std::time(NULL);

std::eek:stringstream output;

// Put the time in a string
output << ctime(&now);

// show the string
std::cout << output.str();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top