Solved

Char to SYSTEMTIME

Posted on 1998-10-15
11
637 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

770 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