fprintf 和 ctime 无需从 ctime 传递 \n

发布于 2025-01-01 07:04:09 字数 849 浏览 1 评论 0原文

我在文本文件中插入时间时遇到问题。我使用以下代码,得到 |21,43,1,3,10,5| Wed Feb 01 20:42:32 2012 这是正常的,但我想做的是将时间放在数字之前,例如 Wed Feb 01 20:42:32 2012 |21,43,1 ,3,10,5| 但是,我不能这样做,因为当我在 fprintf 之前使用带有 ctime 函数的 fprintf 时,它会识别其中的 \n ctime,因此它更改第一行,然后打印数字。它是这样的:

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

这是我不想要的东西......我怎样才能在不切换到文本中的下一行的情况下打印时间???提前致谢!

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));

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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

失眠症患者 2025-01-08 07:04:09

您可以结合使用 strftime() 和 < a href="http://linux.die.net/man/3/ctime" rel="nofollow noreferrer">localtime() 创建时间戳的自定义格式字符串:

char s[1000];

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

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

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

使用的格式字符串ctime 就是"%c\n"

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, sizeof s, "%A, %B %d %Y", p);

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

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

奢望 2025-01-08 07:04:09

您可以使用 strtok()\n 替换为 \0。这是一个最小的工作示例:

#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;
}

输出:

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

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]
初见终念 2025-01-08 07:04:09

只需使用 %.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

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
瑶笙 2025-01-08 07:04:09
  1. ctime() 的返回值复制到临时字符串,从该临时字符串中删除 '\n',然后打印该临时字符串。
  2. 使用 printf 转换的(字段宽度和)精度打印 ctime() 返回的前 24 个字符。
  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.
情徒 2025-01-08 07:04:09

在 c++11 中,你可以这样做:

#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;
}

输出:

2017-06-05 00:31:49

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

泛滥成性 2025-01-08 07:04:09

怎么样:

char *p;
int len;

/* ... */

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

这样它只打印减去最后一个字符的字符串(即 \n)。

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).

小嗷兮 2025-01-08 07:04:09

我在获取 ctime 字符串后执行了此操作:

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

这只是用字符串终止字符覆盖 ctime 回车符,有效地用两个“\0”字符而不是一个字符终止字符串。 (ctime 首先这样做似乎很奇怪。)

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.)

逆夏时光 2025-01-08 07:04:09

只需将“length - 1”字节复制到另一个字符串即可。

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

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

strncpy( newString, draw_No, strlen(draw_no) - 1 );
触ぅ动初心 2025-01-08 07:04:09

简单地说:

    c_time_string = ctime(¤t_time);
    len_of_new_line = strlen(c_time_string) - 1;
    c_time_string[len_of_new_line] = '\0';

这实际上会做的是用空终止符替换 ctime 数组的 strlen - 1 个字符(在本例中为新行字符) - 它从末尾 '\n' 中删除新行字符并缩短 1 个字符的数组。

如果 strlen 之前是 25,那么之后应该是 24。

Simply:

    c_time_string = ctime(¤t_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文