Question

I have an issue with inserting time in a text file. I use the following code and i get |21,43,1,3,10,5| Wed Feb 01 20:42:32 2012 which is normal but what i WANT TO DO is place the time before the numbers for example like Wed Feb 01 20:42:32 2012 |21,43,1,3,10,5| However, i cant do so cause when i use the fprintf with ctime function before fprintf the numbers it recognizes the \n within ctime and so it changes line 1st and then printing the numbers. It goes like:

    Wed Feb 01 20:42:32 2012
    |21,43,1,3,10,5|

which is something that i dont want... How can i fprintf the time without swiching to the next line in the text??? Thanks in advance!

fprintf(file,"   |");
    for (i=0;i<6;i++)
    {
        buffer[i]=(lucky_number=rand()%49+1);       //range 1-49
        for (j=0;j<i;j++)                           
        {
            if (buffer[j]==lucky_number)
                i--;
        }
        itoa (buffer[i],draw_No,10);
        fprintf(file,"%s",draw_No);
        if (i!=5)
            fprintf(file,",");
    }
    fprintf(file,"|     %s",ctime(&t));
Was it helpful?

Solution

You can use a combination of strftime() and localtime() to create a custom formatted string of your timestamp:

char s[1000];

time_t t = time(NULL);
struct tm * p = localtime(&t);

strftime(s, 1000, "%A, %B %d %Y", p);

printf("%s\n", s);

The format string used by ctime is simply "%c\n".

OTHER TIPS

Just use %.19s :

struct timeb timebuf;
char *now;

ftime( &timebuf );
now = ctime( &timebuf.time );

/* Note that we're cutting "now" off after 19 characters to avoid the \n
that ctime() appends to the formatted time string.   */
snprintf(tstring, 30, "%.19s", now);  // Mon Jul 05 15:58:42
  1. Copy the return of ctime() to a temporary string, remove the '\n' from that temporary string, then print the temporary string.
  2. Print just the 1st 24 characters of the return from ctime() by using the (field width and) precision of the printf conversion.

You can use strtok() to replace \n with \0. Here's a minimal working example:

#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
    char *ctime_no_newline;
    time_t tm = time(NULL);

    ctime_no_newline = strtok(ctime(&tm), "\n");
    printf("%s - [following text]\n", ctime_no_newline);

    return 0;
}

Output:

Sat Jan  2 11:58:53 2016 - [following text]

in c++11 you can do it like this:

#include <iostream>
#include <chrono>
#include <iomanip>
using namespace std;
using namespace chrono;

// Prints UTC timestamp
void printTime() {
    time_point<system_clock> now = system_clock::now();
    time_t now_time = system_clock::to_time_t(now);

    auto gmt_time = gmtime(&now_time);
    auto timestamp = std::put_time(gmt_time, "%Y-%m-%d %H:%M:%S");
    cout << timestamp << endl;
}

Output:

2017-06-05 00:31:49

How about:

char *p;
int len;

/* ... */

p = ctime(&t);
len = strlen(p);
fprintf(file,"|     %.*s", len - 1, p);

That way it only prints the string minus the last character (i.e. the \n).

I did this after obtaining the ctime string:

#include <string.h>
...
myctime[ strlen(myctime) - 1 ] = '\0';

This just overwrites the ctime carriage return with a string termination character, effectively terminating the string with two '\0' characters instead of one. (It seems weird that ctime does that in the first place.)

Just copy 'length - 1' bytes to another string.

strncpy( newString, draw_No, strlen(draw_no) - 1 );

Simply:

    c_time_string = ctime(&current_time);
    len_of_new_line = strlen(c_time_string) - 1;
    c_time_string[len_of_new_line] = '\0';

What this will actually do is it replaces strlen - 1 char (new line char in this case) of ctime array with null-terminator character - it cuts out new line character from end '\n' and shorten array of 1 character.

If strlen was 25 before, after this it should be 24.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top