Get stdin into GInputStream


Is there a (easy or not) way to read stdin (FILE *) into GInputStream.
Indeed, my code want to read both from a FILE or from stdin.
From a file it is quite straightforward:

GError *error;
GFile *file = g_file_new_for_path(main_option_script);
input_stream = (GInputStream *)g_file_read(file, NULL, &error);

but not from stdin.

Cheers, and happy new year!

You can try g_unix_input_stream_new_from_fd(fileno(stdin), FALSE);. You might need to #include <gio/gunixinputstream.h> for that function

The FALSE is for not closing stdin when closing the input stream.

1 Like

Hello thx for the answer.

That looks great, but only for UNIX system :/. No portable solution?

Anything that supports POSIX will work with that.

1 Like

Thank you both.
On windows I will use:

However, even with checking

Run-time dependency glib-2.0 found: YES 2.66.4
Run-time dependency gio-2.0 found: YES 2.66.4
Run-time dependency gio-windows-2.0 found: YES 2.66.4

I have

fatal error: gio/gwin32inputstream.h: No such file or directory

EDIT: oh probably need to add include path in configure files.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.