Any suggestions?
---------------------------------------------------------------------
#define _XOPEN_SOURCE /*glibc2 needs this*/
#define _XOPEN_SOURCE_EXTENDED
#include <stdlib.h>
#include <time.h>
char * strptime(const char *, const char *,struct tm *);
int main(int argc, char *argv[])
{
tm *tmBeginDate;
tm *tmEndDate;
string fmt = "%Y %m %d";
string strBeginDate = "2002 5 5";
string strEndDate= "2002.2.5";
strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
return 0;
}
Thanks
C. Denis
--
comp.lang.c.moderated - moderation address: cl...@plethora.net
> [question about strptime()]
strptime() is not a standard C function. Please ask your
question in a newsgroup where it is on-topic. I think
comp.unix.programmer might be an appropriate place, but check
their FAQ and/or charter first.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Don't write in C++ and ask for help in a C group.
> ---------------------------------------------------------------------
> #define _XOPEN_SOURCE /*glibc2 needs this*/
'glibc2' is not a part of standard C. Maybe you should be
looking at a Unix or Posix programming group instead.
> #define _XOPEN_SOURCE_EXTENDED
>
> #include <stdlib.h>
> #include <time.h>
>
>
> char * strptime(const char *, const char *,struct tm *);
Very bad form to define library functions in your source
code. you should include header files to do this for you.
Meanwhile, this function is not, I believe, part of the
standard C library, and you might be better served asking
on a Unix/Posix programming group.
>
>
> int main(int argc, char *argv[])
> {
>
>
> tm *tmBeginDate;
> tm *tmEndDate;
Yuck. Sorta-kinda like hungarian notation, only different.
What is tmEndDate for? It is never used in the code.
>
> string fmt = "%Y %m %d";
> string strBeginDate = "2002 5 5";
> string strEndDate= "2002.2.5";
What is 'strEndDate' for? It is never used in the code.
What is 'string'? No such data type exists in 'C',
and you haven't done anything to define it. Or is this
really a C++ program, which puts you off topic for this
news group? But no, you haven't included anything
to define it in C++ either.
>
> strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
What is this 'strBeginDate.c_str()'? You never initialize
this in what I assume is a structure. You should really
include the definition of 'string' if you want us to
decode your program. Same thing for 'fmt.c_str()'.
Why are you going through all the trouble of creating
'string' variables when the function wants 'char*'s?
Obfuscation?
Where to you set up tmBeginDate? You allocate storage for a
pointer to it, but never get around to allocating storage
for this structure.
> return 0;
The Gnu libc is off-topic in comp.lang.c and comp.lang.c.moderated,
although the C standard library is topical here.
> Im trying to use the following fonction in one of my application, but
> i can't seem to get it right.
>
> Any suggestions?
Suggestion #1: don't post C++ code to comp.lang.c or
comp.lang.c.moderated.
Suggestion #2: don't post questions about non-standard, system
specific extensions to comp.lang.c or comp.lang.c.moderated.
> ---------------------------------------------------------------------
> #define _XOPEN_SOURCE /*glibc2 needs this*/
> #define _XOPEN_SOURCE_EXTENDED
>
> #include <stdlib.h>
> #include <time.h>
>
>
> char * strptime(const char *, const char *,struct tm *);
There is no such function as strptime() in the standard C or C++
library. It is a non-standard extension.
> int main(int argc, char *argv[])
> {
>
>
> tm *tmBeginDate;
> tm *tmEndDate;
The two definitions above are not legal C due to the missing struct
keyword. They are valid if the program is C++.
> string fmt = "%Y %m %d";
> string strBeginDate = "2002 5 5";
> string strEndDate= "2002.2.5";
The three definitions above are not legal C, and not valid C++ either
because the C++ header <string> has not been included.
> strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
There is certainly no such thing as a c_str() member function in C.
> return 0;
> }
>
>
>
> Thanks
> C. Denis
I would suggest you ask this question in
news:comp.os.linux.development.apps.
Include real code when you do. I haven't tried it but I doubt very
much this will compile as written with any version of g++ on Linux,
due to defining string class variables without including the proper
header.
Finally, as always when posting problem code to a programming
newsgroup, be very specific about the problem. The descriptions "not
working" and "can't seem to get it right" do not provide much
information for someone trying to help you.
You need to say:
Does the program compile? If not, what compiler error messages do you
get? If possible, copy and paste the text into your post.
Does the program compile but generator linker errors? If so, copy and
past the messages into your post.
Does the program compile and link but not generate the results you
wanted when you execute it? If so, describe the results you wanted
and the results you get.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
> tm *tmBeginDate;
> tm *tmEndDate;
>
> string fmt = "%Y %m %d";
> string strBeginDate = "2002 5 5";
> string strEndDate= "2002.2.5";
>
> strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
your main problem is that you don't understand pointers, in particular you
have a pointer called `tmBeginDate' but it doesn't point to any storage so
strptime is trying to use an indeterminate pointer value which has
undefined behavior.
this is comp.lang.c(.moderated) so my correction will not use c++:
#include <time.h>
int main(void) {
struct tm tmBeginDate, tmEndDate;
char *fmt = "%Y %m %d";
char strBeginDate[] = "2002 5 5";
char strEndDate[] = "2002.2.5"; /* end is before start? */
/* check return value */ strptime(strBeginDate, fmt, &tmBeginDate);
/* this should succeed, but only tm_year, tm_mon, tm_mday, tn_wday
and tm_yday will be valid */
/* check return value */ strptime(strEndDate, fmt, &tmEndDate);
/* this will fail, 2002.2.5 doesn't match `%Y %m %d' */
return 0;
}
--
bringing you boring signatures for 17 years
Post to a newsgroup where non-standard functions (strptime) is topical.
Post to a newsgroup where the macros _XOPEN_SOURCE and
_XOPEN_SOURCE_EXTENDED have some meaning.
Post to a newsgroup where the type 'tm' has a meaning; it's 'struct tm' here.
Post to a newsgroup where the type 'string' has a meaning; it's '[const]
char *' or '[const] char[]' here.
Post to a newsgroup where the member function c_str() for the bogus type
'string' has a meaning.
Pay attention when told, as you have been several times, that your non-C
crap doesn't belong here.
Ask C++ questions in a C++ news group?
> ---------------------------------------------------------------------
> #define _XOPEN_SOURCE /*glibc2 needs this*/
> #define _XOPEN_SOURCE_EXTENDED
>
> #include <stdlib.h>
> #include <time.h>
>
> char * strptime(const char *, const char *,struct tm *);
If you include the header, you don't need to declare the function.
> int main(int argc, char *argv[])
> {
> tm *tmBeginDate;
> tm *tmEndDate;
A hint this must be C++; in C, you'd have to say 'struct tm *'.
tmEndDate is unused in the code shown; similarly for strEndDate below.
> string fmt = "%Y %m %d";
> string strBeginDate = "2002 5 5";
> string strEndDate= "2002.2.5";
Neither <stdlib.h> nor <time.h> declares a type string. Shouldn't you
include an appropriate C++ header?
The format should be able to convert strBeginDate; it will not convert
strEndDate.
> strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
Ignoring the rampant C++ notation...
You haven't checked whether the conversion succeeded or not, nor found
out where the conversions stopped. Test the result of the function.
You haven't shown how you know that it isn't working. You haven't
shown how you test the results of the conversion. What actually is
your problem? "Not right" covers a lot of ground -- what are you
seeing as the trouble?
> return 0;
> }
You avoided the booby traps of a non-int main. These days, you are
supposed to be able to get away without an explicit return from main -
certainly in C++ and unless my brain is addled (it sometimes is), also
in C99. However, it is my strong preference to see an explicit return
from main().
Happy Christmas.
--
Jonathan Leffler #include <disclaimer.h>
Email: jlef...@earthlink.net, jlef...@us.ibm.com
Guardian of DBD::Informix 1.04.PC1 -- http://dbi.perl.org/
> Im trying to use the following fonction in one of my application, but
> i can't seem to get it right.
>
> Any suggestions?
> ---------------------------------------------------------------------
> #define _XOPEN_SOURCE /*glibc2 needs this*/
Then, bless its buttons, glibc2 is an unconforming abomination. Dump it.
> #include <stdlib.h>
> #include <time.h>
>
> char * strptime(const char *, const char *,struct tm *);
strptime() is not ISO. It's probably one of those "portable, open code"
extensions which tie you in to GNU as firmly as farmalloc() ties you to
VC. Use with care, and ask about it in a compiler-specific newsgroup;
it's off-topic here.
Richard
>Im trying to use the following fonction in one of my application, but
>i can't seem to get it right.
>
>Any suggestions?
Decide whether you are using C or C++!
>---------------------------------------------------------------------
>#define _XOPEN_SOURCE /*glibc2 needs this*/
>#define _XOPEN_SOURCE_EXTENDED
>
>#include <stdlib.h>
>#include <time.h>
/* for C++ use: */
>#include <cstdlib>
>#include <ctime>
>
>
>char * strptime(const char *, const char *,struct tm *);
>
>
>int main(int argc, char *argv[])
>{
> tm *tmBeginDate;
> tm *tmEndDate;
/* you have to allocate storage somewhere */
struct tm tmBeginDate;
struct tm tmEndDate;
/* where is string defined? */
/* isn't this a C++ type? */
> string fmt = "%Y %m %d";
> string strBeginDate = "2002 5 5";
> string strEndDate= "2002.2.5";
const char fmt[] = "%Y %m %d";
const char strBeginDate[] = "2002 5 5";
/* you may need dots in your format to process this */
const char strEndDate[] = "2002.2.5";
>
>
> strptime(strBeginDate.c_str(),fmt.c_str(),tmBeginDate);
/* pass pointer to allocated storage */
strptime( strBeginDate, fmt, &tmBeginDate);
/* do something with begin date */
strptime( strEndDate, fmt, &tmEndDate);
/* do something with end date */
> return 0;
>}
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada
--
Brian....@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply
ab...@aol.com tos...@aol.com ab...@att.com ab...@earthlink.com
ab...@hotmail.com ab...@mci.com ab...@msn.com ab...@sprint.com
ab...@yahoo.com ab...@cadvision.com ab...@shaw.ca ab...@telus.com
ab...@ibsystems.com u...@ftc.gov spam traps
>strptime() is not ISO.
yes it is, just not 9899.
>It's probably one of those "portable, open code"
>extensions which tie you in to GNU
nope.
--
bringing you boring signatures for 17 years
You really need to provide more information than "I can't seem to get
it right". Some indication of what you're trying to do and what
errors you're seeing would be a good start.
I do see a few problems with your code.
It appears to be C++, not C. <time.h> declares a type "struct tm";
you can refer to it as just "tm" in C++, but not in C. C doesn't have
a predefined type "string" (nor does C++ unless you include the header
that defines it).
The strptime() function (which is not defined in the C standard, and
is therefore strictly off-topic for these newsgroups) is defined, when
it's defined at all, in <time.h>. You don't need to redeclare it.
I don't know whether you really need to define _XOPEN_SOURCE and
_XOPEN_SOURCE_EXTENDED in your own program.
You don't initialize tmEndDate, which is ok, since you never use it.
You don't initialize tvBeginDate, which is a problem, since you pass
it to strptime (which stores a value in the "struct tm" object pointed
to by its third argument).
Fix those problems first. If you still have problems and they're
specifically related to the strptime() function, you might try
comp.unix.programming; if you have C questions, feel free to ask them
here (though cross-posting to both comp.lang.c and
comp.lang.c.moderated is probably not necessary).
--
Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
> in comp.lang.c.moderated i read:
>
> >strptime() is not ISO.
>
> yes it is, just not 9899.
It may be ISO Pascal for all I care, it's still not ISO C.
> >It's probably one of those "portable, open code"
> >extensions which tie you in to GNU
>
> nope.
Well, then, which tie you in to ISO Prolog. Or anything else which isn't
ISO C.
Richard
>> >strptime() is not ISO.
>>
>> yes it is, just not 9899.
>
>It may be ISO Pascal for all I care, it's still not ISO C.
i agree. but your answer was so terse and since clc.moderated tolerates
discussion that was not directly centered only on the c language i felt it
necessary to point out that there are other standards, some of which even
touch on the c language and which can be useful and which define that
function.
--
bringing you boring signatures for 17 years
>in comp.lang.c.moderated i read:
>>those who know me have no need of my name <not-a-rea...@usa.net>
>>wrote:
>>> in comp.lang.c.moderated i read:
>
>>> >strptime() is not ISO.
>>>
>>> yes it is, just not 9899.
>>
>>It may be ISO Pascal for all I care, it's still not ISO C.
>
>i agree. but your answer was so terse and since clc.moderated tolerates
>discussion that was not directly centered only on the c language
In that case the correct response would be to trim the crossposting
rather than posting erroneous info in CLC.
Just my ten pennorth, no offense intended.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
RB> strptime() is not ISO.
twkmhnnomn> yes it is, just not 9899.
RB> It may be ISO Pascal for all I care, it's still not ISO C.
twkmhnnomn> i agree. but your answer was so terse and since clc.moderated tolerates
twkmhnnomn> discussion that was not directly centered only on the c language
MM> In that case the correct response would be to trim the crossposting
MM> rather than posting erroneous info in CLC.
Which part of ``strptime() is ISO, just not 9899'' is
erroneous?!?
Tak-Shing