Solved

Char to SYSTEMTIME

Posted on 1998-10-15
11
632 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
  • 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now