简体   繁体   中英

Autostart application in Linux, output fail

friends. I am using Debian Linux (Raspberry Pi), I want to autostart a program after linux startup. 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. After I got exe file, I run command

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

Then I create a new folder "autostart" in /home/username/.config Then I run command

 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. I delete fprintf in code, redo everything, Program can run successful and can printf results. so problem is fprintf(I want to output results to a txt file)! 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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