简体   繁体   English

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

[英]Autostart application in Linux, output fail

friends. 朋友们。 I am using Debian Linux (Raspberry Pi), I want to autostart a program after linux startup. 我正在使用Debian Linux(Raspberry Pi),我想在Linux启动后自动启动程序。 It's a C program, it can printf on Terminal and fprintf on a text file, I have compiled it and got exe file(file name is test) Path is /home/username/try/test ,the program can run successfully, printf and fprintf can work. 这是一个C程序,可以在终端上运行printf,在文本文件上运行fprintf,我已经对其进行编译并得到exe文件(文件名为test),路径为/ home / username / try / test,程序可以成功运行,printf和fprintf可以工作。 After I got exe file, I run command 取得exe档案后,执行命令

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

Then I create a new folder "autostart" in /home/username/.config Then I run command 然后在/home/username/.config中创建一个新文件夹“ autostart”,然后运行命令

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

I continue to write desktop file: 我继续写桌面文件:

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

After this, I reboot. 之后,我重新启动。 the program can autostart, but when the program start to fprintf, the program quit. 程序可以自动启动,但是当程序启动到fprintf时,程序退出。 I delete fprintf in code, redo everything, Program can run successful and can printf results. 我在代码中删除了fprintf,重做了所有程序,程序可以成功运行并且可以执行printf结果。 so problem is fprintf(I want to output results to a txt file)! 所以问题是fprintf(我想将结果输出到txt文件)! I tried many ways and can't solved. 我尝试了许多方法,但无法解决。 I need your suggestions, thanks! 我需要您的建议,谢谢!

I did fprintf as the following: (I run the program normally (Not Autostart), it can work.If autostart, program will quit) 我按以下方式执行了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); 

Have you tried to do it like this?: 您是否尝试过这样做?

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM