Solved

Char to SYSTEMTIME

Posted on 1998-10-15
11
640 Views
Last Modified: 2013-12-03
I am reading a date in form DD/MM/YYYY as a string and trying to allocate to a SYSTEMTIME structure ie; rLogSystemtime.wDay = atoi(cLogfile_day);
wDay is classed asa WORD in the help so how do I get such a conversion to work ?
0
Comment
Question by:ssherlock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1415269
rLogSystemtime.wDay = (WORD) atoi(cLogfile_day);

let me know if you have questions.
0
 

Author Comment

by:ssherlock
ID: 1415270
That causes an illegal operation and the programme dies horribly! (which was what was happening before :(
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415271
The source of problem must occur before that.  Post more of your code.  What is cLogeFile_day set to?
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:ssherlock
ID: 1415272
<Red face> You're right - the command did actually work but I was using printf to see the result and was trying to display a character (%s) rather than integer (%d).  Thanks for your help, now sorted (what's sadder is that I had done it correctly in the first place :))
0
 

Author Comment

by:ssherlock
ID: 1415273
nietod,
Have you any idea why the following fails ?       

SYSTEMTIME rSystemtime;
SYSTEMTIME rLogSystemtime;
FILETIME rFiletime_sys;
FILETIME rFiletime_log;

rLogSystemtime.wDay = (WORD) atoi(cDay);
rLogSystemtime.wMonth = (WORD) atoi(cMonth);
rLogSystemtime.wYear = (WORD) atoi(cYear);

/* Convert SYSTEMTIME structure to FILETIME structure*/
SystemTimeToFileTime(&rLogSystemtime,&rFiletime_log);

GetLastError() returns a "Cannot Create A File When That File Already Exists" - no files are involved (I'm using it for date calculations) ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415274
In what way is it failing?  What are cDay, cMonth, and cYear?  What is the return value from SystemTimeToFileTime()?
0
 

Author Comment

by:ssherlock
ID: 1415275
cDay etc are bits taken from a file so cDay may be 15, cMonth=10 and cYear=1998.  The SYSTEMTIME structure set with these and then converted to a FILETIME structure.  If SystemTimeToFileTime fails it returns FALSE and can be read by using GetLastError() which gives the result I mentioned above.  I'm stumped.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415276
>> cDay etc are bits taken from a file so cDay may be 15, cMonth=10 and cYear=1998
"may be"?  what ARE they?  if these values are not valid, the function will fail.  Have you verified that they are valid?  Have you verified that atoi() has returned the correct value?
0
 

Author Comment

by:ssherlock
ID: 1415277
Sorry, they are int, or rather (WORD) int.  I can sprintf(somestring,"%d/%d/%d",cDay,cMonth,cYear) with no problem.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415278
You are missing the point.  You need to verify that the VALUES STORED IN the month day and  year are valid.  If month is set to 13, the function will fail.  There is no 13th month.  etc.  Also make sure the year specifies the century.  It should be 1988 for the current year, not just 88.

Work backwards.  Check every one of the values in the SYSTEMTIME structure.  They must all be valid other than the day of the week.  (This includes the time members, I assume you are setting them as well.)  If any of these are invalid, the function will not work.
0
 

Author Comment

by:ssherlock
ID: 1415279
You were right.  The values I was using were OK but I had forgotten to initialise the times (wHour, wMinute, wSecond and wMilliseconds).  Once I had allocated 0 to each of those it worked a treat.

Thanks for your help and patience!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question