gpt4 book ai didi

c - Linux中自动启动应用程序,输出失败

转载 作者:行者123 更新时间:2023-11-30 17:41:27 25 4
gpt4 key购买 nike

friend 们。我使用的是 Debian Linux (Raspberry Pi),我想在 Linux 启动后自动启动一个程序。 这是一个C程序,它可以在终端上printf和在文本文件上fprintf,我已经编译它并得到exe文件(文件名是test)路径是/home/username/try/test,程序可以成功运行,printf和fprintf 可以工作。获得 exe 文件后,我运行命令

  sudo chmod +x /home/usernane/try/test

然后我在/home/username/.config 中创建一个新文件夹“autostart”然后运行命令

 cd /home/username/.config/autostart
sudo nano test.desktop

我继续写桌面文件:

 [Desktop Entry]
Name=test
exec=lxterminal -e "/home/username/try/test"
Type=Application

此后,我重新启动。程序可以自动启动,但是当程序启动到fprintf时,程序退出。我删除代码中的fprintf,重做所有内容,程序可以成功运行并可以打印结果。所以问题是fprintf(我想将结果输出到txt文件)!我尝试了很多方法都无法解决。我需要您的建议,谢谢!

我做了fprintf如下:(我正常运行程序(不自动启动),它可以工作。如果自动启动,程序将退出)

FILE *fp;
char results[50]

/* check if file could be opened */
if((fp=fopen("xy.txt", "w")) == NULL) { // or use "a" instead of "w" to create the file if it doesn't exist
printf("Cannot open file.\n");
exit(1);
}
/* put your results into results[] */
....
/* afterwards writing to file */
fprintf(fp, "%s", results);
fclose(fp);

最佳答案

你尝试过这样做吗?:

FILE *fp;
char results[50]

/* check if file could be opened */
if((fp=fopen("test.txt", "w")) == NULL) { // or use "a" instead of "w" to create the file if it doesn't exist
printf("Cannot open file.\n");
exit(1);
}
/* put your results into results[] */
....
/* afterwards writing to file */
fprintf(fp, "%s", results);
fclose(fp);

关于c - Linux中自动启动应用程序,输出失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21105965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com