fprintf 和 ctime 无需从 ctime 传递 \n
我在文本文件中插入时间时遇到问题。我使用以下代码,得到 |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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以结合使用
strftime()
和 < a href="http://linux.die.net/man/3/ctime" rel="nofollow noreferrer">localtime()
创建时间戳的自定义格式字符串:使用的格式字符串
ctime
就是"%c\n"
。You can use a combination of
strftime()
andlocaltime()
to create a custom formatted string of your timestamp:The format string used by
ctime
is simply"%c\n"
.您可以使用
strtok()
将\n
替换为\0
。这是一个最小的工作示例:输出:
You can use
strtok()
to replace\n
with\0
. Here's a minimal working example:Output:
只需使用 %.19s :
Just use %.19s :
ctime()
的返回值复制到临时字符串,从该临时字符串中删除'\n'
,然后打印该临时字符串。ctime()
返回的前 24 个字符。ctime()
to a temporary string, remove the'\n'
from that temporary string, then print the temporary string.ctime()
by using the (field width and) precision of the printf conversion.在 c++11 中,你可以这样做:
输出:
in c++11 you can do it like this:
Output:
怎么样:
这样它只打印减去最后一个字符的字符串(即
\n
)。How about:
That way it only prints the string minus the last character (i.e. the
\n
).我在获取 ctime 字符串后执行了此操作:
这只是用字符串终止字符覆盖 ctime 回车符,有效地用两个“\0”字符而不是一个字符终止字符串。 (ctime 首先这样做似乎很奇怪。)
I did this after obtaining the ctime string:
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.)
只需将“length - 1”字节复制到另一个字符串即可。
Just copy 'length - 1' bytes to another string.
简单地说:
这实际上会做的是用空终止符替换 ctime 数组的 strlen - 1 个字符(在本例中为新行字符) - 它从末尾 '\n' 中删除新行字符并缩短 1 个字符的数组。
如果 strlen 之前是 25,那么之后应该是 24。
Simply:
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.