Jessica Chambers
Jessica Chambers

Reputation: 1316

Opening image files through unix system command in C

I have a project where the user can open files by selecting them in a menu. I have two near identical pieces of code, but one works, whereas the other doesn't: the one that works is for opening text files through gedit ("chemin" contains the file path):

char buf[200];
snprintf(buf,sizeof(buf),"gedit %s",chemin);
system(buf);

And this one doesn't work when run in my code, but does work when run outside of it (opens .jpg files with eog - have also tried xdg with no improvement):

snprintf(buf,sizeof(buf),"eog %s",chemin);
system(buf);

Is there a surer way of opening .jpg files from the unix command line? Or did I forget something? TIA

UPDATE It seems the buffer only prints its first 7 characters to the command line, ie: my file path: ./FICHIER_PROJET/basededonnee/basedeDonneefichier/IMG_RGB/21.jpg what the command line prints: eog ./FI This only happens with these .jpg files

Upvotes: 0

Views: 241

Answers (2)

Jessica Chambers
Jessica Chambers

Reputation: 1316

Solved it... I had been using one general buffer for all my system commands. I created a new buffer just for this one and it works.

Upvotes: 0

Romain Artru
Romain Artru

Reputation: 65

The problem might come from the char '\0' that has the wrong place, try something like :

strncpy(buf, "eog ", 4);
strncat(buf, chemin, sizeof(chemin));
buf[4+sizeof(chemin)] = '\0';
if(system(buf) == -1){
    perror("Error with the system call ");
    exit(-1);
}

Upvotes: 1

Related Questions