Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using

C++
time_t time1 = number;
                    ctime(*time1);

to print the time in the variable to a better readable format.

However the returned string has the following format:

Www Mmm dd hh:mm:ss yyyy 


The string is followed by a new-line character ('\n') and the terminating null-character.

I want to remove the newline character as well as hh:mm:ss from the same.

Is it possible?
Posted
Updated 24-Sep-11 4:40am
v2
Comments
Richard MacCutchan 24-Sep-11 10:09am    
I see that you are still struggling with basic concepts and continue to ignore my advice to go and do some studying of C++ before trying to attempt things that you really do not seem to understand.
typedefcoder 24-Sep-11 10:11am    
I am still working over it.. but i have some stuff to complete.. its been all over C++ i have been doing... Thanks for your advice
Chuck O'Toole 24-Sep-11 10:25am    
The null at the end of the string is your friend, leave it. You'll thank me later.
typedefcoder 24-Sep-11 10:32am    
i need to use the particular values within that ctime output for a display. So i need to curtail \n . thanks chuck for the advice. :)
Chuck O'Toole 24-Sep-11 10:37am    
Your question says you want to remove the null at the end. \n is *not* the null, it is the newline.

[...] edit: cut away part that no longer applies

Some advice:

1. Read the full reference on the function ctime() and make sure you understand it. Here's a link : http://www.cplusplus.com/reference/clibrary/ctime/ctime/[^]

2. The buffer containing the string that ctime() returns is statically allocated and shared with other functions within this library. it may therefore be overwritten later and you cannot rely on it to maintain any modifications you apply to it. More importantly, you do not know which functions access the buffer and in what way - any modification you make - and especially overwriting the terminating 0 - may well cause them to behave abnormally or even crash your app!

3. In short, never try to modify a C style string returned from a library function. At least not unless the function documentation explicitely states that you may do so. And even then, do not do it unless you understand C style strings: read a book or tutorial on how to use them!

4. To address your problem, if you need a modified version of the output of ctime(), do the following:
- determine the total length of the modifed string
- add 1 for the terminating 0 character
- allocate a new C style string of that length
- copy the bits of the ctime() return value that you want into that new string.
- add the bits that you want modified
- when you're done with that modified string, release it from memory

I'm not going to write the actual code for that, because before I do I want you to read up on C strings and the string functions. Theere's no point in giving you the code if you fail to understand the meaning of every single function therein.
 
Share this answer
 
v2
Comments
typedefcoder 26-Sep-11 4:33am    
thanks my issue was resolved. i converted the ctime output to string. and then took a loop to copy each characters i wanted in an empty string and concatenate it in the loop.
This way i had to take 2 loops to trim off the value of hh:mm:ss

thanks for your help anyways.
Richard MacCutchan 26-Sep-11 5:41am    
i had to take 2 loops to trim off the value of hh:mm:ss.
Something that could be done with a simple copy or substring method.
typedefcoder 26-Sep-11 5:45am    
yes.. i took a similar kind of approach earlier to solve this.... steadily i m getting into c++ ... i hope to learn more from you folks.
You have to copy the wanted part of the original string to a new one.
Stripping of the terminating null character doesn't look a good idea but, anyway yes, you may do it.
 
Share this answer
 
Comments
Chuck O'Toole 24-Sep-11 10:40am    
Why did somebody downvote this? It's a perfectly reasonable response. That OP wants to strip the hh:mm:ss portion, the advice to locate the string portion you want and move it somewhere else is correct. Or were you looking for the exact strncpy() command to do it for you? Do youself a favor, much of C coding is character fondling, figure out how char * based strings work and get used to it.

Here's an offsetting 5.
CPallini 24-Sep-11 13:41pm    
Thank you.
You can potentially use just printf() for this.

For example, if you only need a substring of the output, and you're printing with printf(), you can use the format qualifiers to select a portion of the result.
If you want to strip the trailing newline, you can do something like:
C
time_t tm = time(NULL);
printf("The time is '%.24s'\n", ctime(&tm));

Which selects the first 24 characters from the output (the newline is char 25).
It should give a result like:
The time is 'Mon 2 Oct 12:18:33 BST 2023'

Or if you wanted just the HH:MM:SS from the output, you could use:
C
time_t tm = time(NULL);
printf("The time is '%.8s'\n", ctime(&tm)+11);

This selects 8 characters, starting at offset 11.
It should give a result like:
The time is '12:18:33'

This works (and is safe) because ctime() uses a fixed output format.
 
Share this answer
 
v2
Comments
OriginalGriff 2-Oct-23 7:25am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 12 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.
Mordac the Preventer 2-Oct-23 7:34am    
I was looking to solve the same problem, and searching for other people's solutions returned this as the first answer. After thinking 'there must be a better way than that', I realised that printf() would do the work for me, so I added it as a potential solution. I didn't look at the age of the question, and when I entered a comment there was no suggestion that I should not do so.

I don't understand the purpose of discouraging answers to old questions - if there's a better solution it's worth listing it regardless of the age of the question, otherwise your database will fill up with old superfluous answers - but if you want to discourage "rep point hunting", perhaps the policy should be that "old questions" don't score you any "points".

Or you could manually remove the points if you think thats the motive for the post.

Or you could just criticise someone for their first posted answer and discourage them for ever using 'Code Project' ever again 🤷

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900