首先,我知道有一个同名的问题,但它涉及c++而不是c。
有没有办法在c中将字符串设置到剪贴板中?
这是提到的问题,如果有人感兴趣,尽管它是为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。