2

I have a a simple question, if I want to print a value on the same line as the output of my system time, is this possible?

char *date;
time_t timer;
timer=time(NULL);
date = asctime(localtime(&timer));
//printf("Current Date: %s", date);


  std::cout << date << ", " << randomNumber  << std::endl;

  if (file.is_open())
  {
    file << date;
    file << ", ";
    file << randomNumber;
    file << "\n";
  }

What I was hoping would happen is that I would get this as an output:

Wed Jan 16 16:18:56 2013, randomNumber

But what I do end up getting in my file is :

Wed Jan 16 16:18:56 2013
, randomNumber

Also, I just did a simple std::cout, and I notice the same result. It seems that the system forces an end line at the end of the output, is there anyway, I can supress this?

c0d3rz
  • 649
  • 3
  • 15
  • 23
  • 1
    You could just use the more general [`strftime`](http://linux.die.net/man/3/strftime) with format string `"℅c"`... – Kerrek SB Jan 17 '13 at 00:47

4 Answers4

8

You can just replace the '\n' character in the date string (if null terminated it should be at strlen(date) - 1) with '\0' and it should print on the same line.

date[strlen(date) - 1] = '\0';

EDIT: As pointed out by Joachim strlen returns length without NULL terminator not raw allocation length so it should be -1 not -2.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • That did work for me Jesus. I had modified it to -1, else it was eating the *3* of 2013. Thanks a lot, I need to wait for the timer to run down before I can accept your answer .. – c0d3rz Jan 17 '13 at 00:27
  • Another quick question Jesus, how can I access merely the milli-second component of the time? – c0d3rz Jan 17 '13 at 00:40
  • @c0d3rz You should use gettimeofday for that It gives you a struct timeval which has microseconds. – Jesus Ramos Jan 17 '13 at 00:42
3

The newline character is the last character in the string returned from asctime. The simplest way to remove it is to replace it with the string terminator character '\0'.

A note about Windows: Windows have two characters for newline, carriage-return '\r' and the regular newline '\n'. So on Windows you have to terminate at the second last character. If you want your code to be portable across Windows and non-Windows platforms you have to add checks for that.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

If you want to use strings you have better alternatives.

This worked for me:

#include<iostream>
#include<chrono>
#include<ctime>

using namespace std;

string getDate()
{
    chrono::system_clock::time_point tp = chrono::system_clock::now();
    time_t t = chrono::system_clock::to_time_t(tp);
    const char * tc = ctime(&t);
    string str = string {tc};
    str.pop_back();
    return str;
}

int main(){
    cout << getDate() << endl << hi;
}

Output:

Mon Dec  7 17:40:01 2015
hi
patrik
  • 4,506
  • 6
  • 24
  • 48
-1

this will work:

date = date.substr(0, date.size()-1);
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Sovan
  • 1
  • 1
  • 2
    Where in the code is that supposed to go? Is it supposed to replace the existing `date =` assignment, go after it, or something else? – Stephen Ostermiller Apr 30 '20 at 10:21
  • why down vote !! according to the questions we need to cut the "\n", which is the reason the automatic newline is occurring. we need to store the date as new srting. – Sovan Aug 08 '20 at 03:25
  • you didn't read the question. yes, this code is suppose to replace the date with out the new line character. if you don't want so store it into another string. – Sovan Aug 08 '20 at 03:29
  • Instead you could write date.pop_back() O(1) solution. – Keshav Sahu Jan 18 '22 at 11:49