使用ctime保存新的时间点会覆盖旧的字符串吗? [英] Saving new points in time with ctime overwrites the old strings?

查看:150
本文介绍了使用ctime保存新的时间点会覆盖旧的字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言:去年夏天我一直在学习C,直到最近才开始学习C ++,所以我对它的了解还不是很多.

Preface: I've been learning C this past summer and only recently started learning C++ so I don't know very much of it yet.

我一直在尝试编写一个C/C ++程序,该程序可以识别Windows中的左右鼠标单击,并保存单击的按钮以及何时进入指向字符串char **clicks的指针数组.我将其组织为一个序列,例如:按钮,时间,按钮,时间...

I've been trying to write a C/C++ program that recognizes right and left mouse clicks in windows and saves which button was clicked and when into an array of pointers to strings char **clicks. I'm organizing it as a sequence like: button, time, button, time...

只要检测到鼠标单击,它就会使用"R"或"L",并且执行此操作:

Whenever a mouseclick is detected, it takes either "R" or "L" and does this:

void writeClick(char *button) {
    static char **tracker = clicks;
    *tracker = button;
    tracker++;
    time_t seconds = time(NULL);
    *tracker = ctime(&seconds);
    tracker++;
}

我的问题:您可能已经注意到的问题是,每个带有时间的数组元素都将指向相同的字符串.结果是时间将在每次单击鼠标时更新,但只会记住上一次保存的时间.我以为我可以用strcpy解决问题,但是如果我想保存多个字符串,那么每个字符串都不需要一个新变量吗?还是每次都有一个新的内存位置?我该怎么办?

My question: The issue you may have noticed is that each array element with the time will be pointing to the same string. The result is that the time will update on each mouse click, but it will only remember the last saved time. I figured I could solve the problem with strcpy, but if I want to save multiple strings then won't I need a new variable for each string? Or a new memory location each time or something? How should I do this?

我尝试像这样使用strcpy:

strcpy(*tracker, ctime(&seconds));

但是在第二次单击时,我的程序崩溃了,我不确定为什么.

But at the second click my program crashes and I'm not sure why.

其他可能有用的信息:

我打算将数据发送到VBA,因为我想将其转换为Excel电子表格.

I'm planning on sending the data into VBA because I want to turn it into an Excel spreadsheet.

我还没有编写代码的一部分,只要它满了就可以动态增加数组,但是目前它足够大(50个元素),可以包含几次单击而不会崩溃的数据,所以我知道可能不是问题.

I've yet to write the part of my code that dynamically increases the array whenever it gets full, but at the moment it's large enough (50 elements) to contain the data for a few clicks without crashing, so I know that's probably not the issue.

我的代码中检测鼠标单击的部分使用Windows函数,除此之外,我认为我的代码不受我使用的操作系统的影响.点击检测没有任何问题(因为我已经解决了所有问题).

The part of my code that detects mouseclicks uses a Windows function and aside from that I don't think my code is affected by which OS I'm using. I don't have any issues with click detection (because I solved them all already).

一个朋友告诉我,她只会使用C ++字符串和向量来完成这项工作,但我还不知道它们.我会查找它们,但我希望找到一种可能的解决方案.

A friend told me she would just use C++ strings and vectors to do the job, but I don't know them yet. I'll look them up but I'm hoping to find a solution without that if possible.

推荐答案

来自ctime

返回值

指向以空值结尾的静态字符串的指针,该字符串包含日期和时间的文本表示形式.该字符串可以在std :: asctime和std :: ctime之间共享,并且在每次调用其中任何一个函数时都可能被覆盖.

Pointer to a static null-terminated character string holding the textual representation of date and time. The string may be shared between std::asctime and std::ctime, and may be overwritten on each invocation of any of those functions.

所以是的,下一次调用ctimeasctime可能会覆盖上一次调用使用的缓冲区.

So yes, the next call to ctime or asctime can be expected to overwrite the buffer used by the previous call.

strcpy(*tracker, ctime(&seconds));

很可能失败,因为没有为*tracker分配任何存储空间来容纳字符串.

Most likely fails because no storage has been allocated for *tracker to hold the string in.

您可以

*tracker = strdup(ctime(&seconds));

进行分配和复制,但这给您留下了跟踪tracker哪些元素必须手动free的问题.这将很快变得令人讨厌.如果您希望使用C执行此操作,则必须全部完成strdupfree,添加簿记结构以告诉您是否动态分配了存储空间,因此必须free,或者执行大量重新设计.

to allocate and copy, but this leaves you with the problem of keeping track of which elements of tracker you have to manually free. This will get incredibly nasty fast. If you wish to do this with C, you will either have to strdup and free everything, add in a book-keeping structure to tell you whether or not you dynamically allocated storage and thus must free, or do an extensive redesign.

由于已标记了C ++,请遵循朋友的建议,并使用std::vectorstd::string.

Since you have tagged C++, follow your friend's advice and use std::vector and std::string.

strdup上的文档.

Documentation on strdup.

std::vector上的文档.

Documentation on std::vector.

std::string上的文档.

Documentation on std::string.

这篇关于使用ctime保存新的时间点会覆盖旧的字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆