|
|
I have a string represent the time and want to convert into time format...
ex) 135621372
I use strptime but it doesn't work ..
I don't want to use the substring method to get hour, minute, and second separtely. It is because I need to do some calculation for the time
struct tm tm;
if (strptime("135621372", "%H%M%S", &tm) != NULL)
{
// do something....
}
Anyone has an idea on how to do the conversion?
Thank you.
modified on Wednesday, November 11, 2009 3:17 PM
|
|
|
|
|
sugarandcream wrote: I have a string represent the time...
ex) 135621372
Based on what? Seconds/milliseconds since some epoch?
sugarandcream wrote: struct tm tm;
Just to be safe, I'd name your variable something other than tm .
sugarandcream wrote: I use strptime but it doesn't work ..
You failed to indicate what "doesn't work" means. If it's because the function does not exist, see here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
the string just represent a time in hour, minute, second, millisecond
135621372 --> hour is 13, minute is 56, second is 21, millisecond is 372
it doesn't work because "tm" in the if statment returns NULL
|
|
|
|
|
sugarandcream wrote: it doesn't work because "tm" in the if statment returns NULL
Can you step into strptime() to find out why?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I trying using the another string separate by ":"
ex) 13:56:21:372
for the if statement, i use
if (strptime("13:56:21:372", "%H:%M:%S:%f", &tm) != NULL)
this time, it does not return NULL
However, the string that I need to parse does not have any delimiter.
Is there any way to convert a string to time format??
thanks..
|
|
|
|
|
Have you considered something like:
char *szTime = "135621372";
strncpy(szHour, szTime, 2);
strncpy(szMinute, &szTime[2], 2);
strncpy(szSecond, &szTime[4], 2);
strcpy(szMsec, &szTime[6]);
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
is that the only way i can do?
since i need to do calculation of the time, such as add and subtract...
for example
time is 001100123 (better way to see is 00:11:00:123)
if I need to subtract an hour from it, the new time is 23:11:00:123
|
|
|
|
|
sugarandcream wrote: is that the only way i can do?
There's more than one way to skin a cat.
sugarandcream wrote: since i need to do calculation of the time, such as add and subtract...
Once you get the time string into a common time structure/format, adding and subtracting is easy.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
In a C++ app I want to send a text message from the main thread to a client thread whose sole job will be to call MessageBox to display the text when it gets a message. Looks like some version of postmessage and getmessage might work, but reading about them seems more confusing than clarifying.
Initially this will be used for exception messages, so the rates would be very, very few, if any, on a given day.
Any suggestions would greatly be appreciated.
Thanks
ak
|
|
|
|
|
Alan Kurlansky wrote: ...client thread whose sole job will be to call MessageBox to display the text when it gets a message.
Not a good idea to do this from a secondary thread. What are you ultimately trying to do?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I want to send a text message from the primary thread to a secondary thread to popup to user, after which they hit ok and the popup text window will disappear. The goal is to not cause the primary thread to halt waiting for the user to click ok, because the primary thread is processing high speed, real time data.
|
|
|
|
|
Alan Kurlansky wrote: The goal is to not cause the primary thread to halt waiting for the user to click ok...
But that's exactly what will happen. The primary thread should handle all user interface items, while the secondary thread should handle all of the data processing stuff.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
You may end with many messages opened in the secondary thread...
But to do that, you must:
Create a window in the secondary thread (to have a handle).
In the primary thread, call PostMessage (as send message blocks waiting for the result)
Pass the string (char *) as the LParam of the PostMessage.
And, of course, in the other thread, when the message arrives, you must cast the LParam to (char *).
That's all.
(ah, I am not sure, but I think there is a PostThreadMessage and some WaitMessage function, but I actually don't use C++ so I can't tell that for sure).
|
|
|
|
|
Alan Kurlansky wrote: because the primary thread is processing high speed, real time data.
That's precisely what worker threads are for! You shouldn't be putting such heavy processing on the primary thread. Your approach is flawed.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hey Guys,
I have an intresting problem for everyone to consider. One of my software projects reads and writes files to the system directory. It automatically detects the location of the Windows system directory, and then uses fopen to either read or write a file depending on what is required.
So here is the conundrum. These techniques work like a charm on my projects that I programmed using Visual C++ v6.0. No problem what so ever. I recently ported this project to Visual Studio 2008 using C++ and MFC, and for XP business as usual. However for Vista, disaster. The nature of the problem is that when I use fopen it can't open the file in the windows system directory. Basically I get (fp = fopen( m_keyPath, "w" or "r" )) == NULL. Here's the twist in the tale. Any where else it's fine. It's just the windows system directory.
Any ideas??
Best Regards
Danny
|
|
|
|
|
Danny Nowlan wrote: These techniques work like a charm on my projects that I programmed using Visual C++ v6.0.
The problem has nothing to do with VS6.
Your program should not be writing/crearing files in the \Windows\System32 folder. That folder has been off limits for years, but since it has not been enforced, folks have continued to abuse it. Use SHGetFolderPath() to get the path of a folder that you can write to. For example, CSIDL_COMMON_APPDATA , CSIDL_LOCAL_APPDATA , CSIDL_COMMON_DOCUMENTS , CSIDL_PERSONAL .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
As normal user you will not get access to the system directory of vista.
On vista and above many security issues changed!
Try your to start your exe "as adminstrator" and you will see it will work.
Greetings
Covean
|
|
|
|
|
Could you explain why you need to read/write files in the System32 directory ?
There is a reason why Microsoft tries to protect its system directories.
|
|
|
|
|
Hey Snakefoot,
I use it as a convenient location to keep operating files without bothering with the registry. Let me give an assurance that I am not in the business of writing viruses or Malware. They may have the best of intentions, but people like this cause alot more harm than they do good.
Danny
|
|
|
|
|
When you mean operating file, then I guess you mean data files that belongs to your application. Such files should not be placed in the Windows-directory.
What if another application suddenly decides to also use this location, and by mistakes uses your filename-format ?
You should read this guide from Microsoft Data and Settings Management[^] (Though written for Win2k, then it also holds for Vista/Win7)
TCHAR szAppData[MAX_PATH];
...
hr = SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szAppData);
PathAppend(szAppData, "Company\Product\File.txt")
- CSIDL_APPDATA - Per user, roaming [user profile]\Application data
- CSIDL_LOCAL_APPDATA - Per user, non-roaming [user profile]\Local Settings\Application data
- CSIDL_COMMON_APPDATA - Hidden folder for normal users. Per machine (non-user specific & non- roaming) All Users\Application data
- CSIDL_COMMON_DOCUMENTS - Visible folder for normal users. Default files in this folder is readonly for other users than the creator, the application must change the security permissions on its application-folder to change this behavior. Per machine (non-user specific & non- roaming) All Users\Documents
modified on Monday, December 14, 2009 10:39 AM
|
|
|
|
|
Hi all,
I am using '_UNICODE' rather '_MBCS' in my MFC single document app.
My problem code-
CString openFilePath;
CFileDialog fileOpen(true, NULL, NULL, NULL, L"Text file(.txt)||*.txt", NULL);
openFilePath = fileOpen.GetPathName(); //it returns CString
'GetPathName()' returns just a single charecter. When i use '_MBCS' it returns full file path.
I want unicode standard with full file path when i open a file using "CFileDialog".
Thanks
|
|
|
|
|
Shaheen.India wrote: 'GetPathName()' returns just a single charecter. When i use '_MBCS' it returns full file path.
How are you verifying this?
Shaheen.India wrote: I want unicode standard with full file path when i open a file using "CFileDialog".
CFileDialog does not open files. It merely allows you to select them.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Verifying in Debug mode. It sends a single character as a string. When i open a file, if it belongs like "e:\MyData\aa.txt", it returns just "e" as string
|
|
|
|
|
Which does not answer my question. If you are using the Watch window, then you'll need to use the ,su symbol to see Unicode strings. Otherwise, you're just going to see a single character.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|