Gomathi
Gomathi

Reputation: 1003

Opening a file externally using C program

I'd like to create a program that causes a running gedit process to open a .txt file in a new tab using C. The file would be the argument I supply to the program. However, I'd like to avoid using system() altogether.

I'm on Linux. Is this possible? If so, by what means?

Upvotes: 1

Views: 2528

Answers (3)

Jeremy List
Jeremy List

Reputation: 1766

You don't need system(). You can use fork/execlp

if(!fork())
    execlp("gedit", "gedit", filename, NULL);

The version of gedit that's on my laptop uses a new tab by default if there's a running instance already, but I'm not sure about other versions.

Upvotes: 1

user1009133
user1009133

Reputation:

Here is a possible solution:

Step 1. Modify gedit source file (function: is_in_viewport() to return TRUE) (see this link).

Step 2. Use fork()/execl() in program and call this modified gedit (there are a few examples here).

Upvotes: 0

Nikos C.
Nikos C.

Reputation: 51850

The standard way to do this is to not hardcode the editor's executable name into your application. Users might not have gedit installed at all (it's mostly only there if the system runs Gnome.) What you do instead is use xdg-open with the file you want to open as its argument. For example:

system("xdg-open myfile.txt");

If the user is instead using gedit as the default editor, this will open the file in a new tab if gedit is already running. If it's not running, it will be started first.

Upvotes: 1

Related Questions