【发布时间】:2014-11-20 23:10:21
【问题描述】:
首先,我知道有一个同名的问题,但它处理的是 c++ 而不是 c。
有没有办法在c中设置一个字符串到剪贴板?
This is the mentioned question if anyone is curious, even though it is for windows.
我需要它在 c 中,因为我正在用 c 编写程序,并且我想将一个字符串复制到剪贴板。
printf("Welcome! Please enter a sentence to begin.\n> ");
fgets(sentence, ARR_MAX, stdin);
//scan in sentence
int i;
char command[ARR_MAX + 25] = {0};
strncat(command, "echo '",6);
strncat(command, sentence, strlen(sentence));
strncat(command, "' | pbcopy",11);
command[ARR_MAX + 24] = '\0';
i = system(command); // Executes echo 'string' | pbcopy
上面的代码除了字符串之外还保存了 2 个新行。 ARR_MAX 为 300。
【问题讨论】:
-
您链接到的问题是针对 Windows 的。您已经为 OS X 标记了您的问题。当然,这些是完全不同的。请澄清你的问题。另外,您能否解释一下为什么使用 C 语言很重要?
-
我添加了一个简短的函数,它完全可以满足您的需求。不使用 strncat ,这对我来说似乎是迟钝的。