📄 glib-io-channels.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>IO Channels</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-core.html" title="GLib Core Application Support"><link rel="prev" href="glib-Memory-Allocation.html" title="Memory Allocation"><link rel="next" href="glib-Error-Reporting.html" title="Error Reporting"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Memory-Allocation.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-core.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-Error-Reporting.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-IO-Channels"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">IO Channels</span></h2><p>IO Channels — portable support for using files, pipes and sockets.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a>;<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a>* <a href="glib-IO-Channels.html#g-io-channel-unix-new">g_io_channel_unix_new</a> (int fd);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-IO-Channels.html#g-io-channel-unix-get-fd">g_io_channel_unix_get_fd</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);void <a href="glib-IO-Channels.html#g-io-channel-init">g_io_channel_init</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a>* <a href="glib-IO-Channels.html#g-io-channel-new-file">g_io_channel_new_file</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, const <a href="glib-Basic-Types.html#gchar">gchar</a> *mode, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-read-chars">g_io_channel_read_chars</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gchar">gchar</a> *buf, <a href="glib-Basic-Types.html#gsize">gsize</a> count, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-read-unichar">g_io_channel_read_unichar</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Unicode-Manipulation.html#gunichar">gunichar</a> *thechar, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-read-line">g_io_channel_read_line</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gchar">gchar</a> **str_return, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Basic-Types.html#gsize">gsize</a> *terminator_pos, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-read-line-string">g_io_channel_read_line_string</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Strings.html#GString">GString</a> *buffer, <a href="glib-Basic-Types.html#gsize">gsize</a> *terminator_pos, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-read-to-end">g_io_channel_read_to_end</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gchar">gchar</a> **str_return, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-write-chars">g_io_channel_write_chars</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, const <a href="glib-Basic-Types.html#gchar">gchar</a> *buf, <a href="glib-Basic-Types.html#gssize">gssize</a> count, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-write-unichar">g_io_channel_write_unichar</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Unicode-Manipulation.html#gunichar">gunichar</a> thechar, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-flush">g_io_channel_flush</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-seek-position">g_io_channel_seek_position</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gint64">gint64</a> offset, <a href="glib-IO-Channels.html#GSeekType">GSeekType</a> type, <a href="glib-Error-Reporting.html#GError">GError</a> **error);enum <a href="glib-IO-Channels.html#GSeekType">GSeekType</a>;<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-shutdown">g_io_channel_shutdown</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gboolean">gboolean</a> flush, <a href="glib-Error-Reporting.html#GError">GError</a> **err);enum <a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a>;enum <a href="glib-IO-Channels.html#GIOChannelError">GIOChannelError</a>;#define <a href="glib-IO-Channels.html#G-IO-CHANNEL-ERROR:CAPS">G_IO_CHANNEL_ERROR</a><a href="glib-IO-Channels.html#GIOChannelError">GIOChannelError</a> <a href="glib-IO-Channels.html#g-io-channel-error-from-errno">g_io_channel_error_from_errno</a> (<a href="glib-Basic-Types.html#gint">gint</a> en);<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a>* <a href="glib-IO-Channels.html#g-io-channel-ref">g_io_channel_ref</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);void <a href="glib-IO-Channels.html#g-io-channel-unref">g_io_channel_unref</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);<a href="glib-The-Main-Event-Loop.html#GSource">GSource</a>* <a href="glib-IO-Channels.html#g-io-create-watch">g_io_create_watch</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a> condition);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-IO-Channels.html#g-io-add-watch">g_io_add_watch</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a> condition, <a href="glib-IO-Channels.html#GIOFunc">GIOFunc</a> func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data);<a href="glib-Basic-Types.html#guint">guint</a> <a href="glib-IO-Channels.html#g-io-add-watch-full">g_io_add_watch_full</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gint">gint</a> priority, <a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a> condition, <a href="glib-IO-Channels.html#GIOFunc">GIOFunc</a> func, <a href="glib-Basic-Types.html#gpointer">gpointer</a> user_data, <a href="glib-Datasets.html#GDestroyNotify">GDestroyNotify</a> notify);enum <a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a>;<a href="glib-Basic-Types.html#gboolean">gboolean</a> (<a href="glib-IO-Channels.html#GIOFunc">*GIOFunc</a>) (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *source, <a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a> condition, <a href="glib-Basic-Types.html#gpointer">gpointer</a> data); <a href="glib-IO-Channels.html#GIOFuncs">GIOFuncs</a>;<a href="glib-Basic-Types.html#gsize">gsize</a> <a href="glib-IO-Channels.html#g-io-channel-get-buffer-size">g_io_channel_get_buffer_size</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);void <a href="glib-IO-Channels.html#g-io-channel-set-buffer-size">g_io_channel_set_buffer_size</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gsize">gsize</a> size);<a href="glib-IO-Channels.html#GIOCondition">GIOCondition</a> <a href="glib-IO-Channels.html#g-io-channel-get-buffer-condition">g_io_channel_get_buffer_condition</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);<a href="glib-IO-Channels.html#GIOFlags">GIOFlags</a> <a href="glib-IO-Channels.html#g-io-channel-get-flags">g_io_channel_get_flags</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-set-flags">g_io_channel_set_flags</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-IO-Channels.html#GIOFlags">GIOFlags</a> flags, <a href="glib-Error-Reporting.html#GError">GError</a> **error);enum <a href="glib-IO-Channels.html#GIOFlags">GIOFlags</a>;const <a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-IO-Channels.html#g-io-channel-get-line-term">g_io_channel_get_line_term</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gint">gint</a> *length);void <a href="glib-IO-Channels.html#g-io-channel-set-line-term">g_io_channel_set_line_term</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, const <a href="glib-Basic-Types.html#gchar">gchar</a> *line_term, <a href="glib-Basic-Types.html#gint">gint</a> length);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-IO-Channels.html#g-io-channel-get-buffered">g_io_channel_get_buffered</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);void <a href="glib-IO-Channels.html#g-io-channel-set-buffered">g_io_channel_set_buffered</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gboolean">gboolean</a> buffered);const <a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-IO-Channels.html#g-io-channel-get-encoding">g_io_channel_get_encoding</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);<a href="glib-IO-Channels.html#GIOStatus">GIOStatus</a> <a href="glib-IO-Channels.html#g-io-channel-set-encoding">g_io_channel_set_encoding</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, const <a href="glib-Basic-Types.html#gchar">gchar</a> *encoding, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-IO-Channels.html#g-io-channel-get-close-on-unref">g_io_channel_get_close_on_unref</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);void <a href="glib-IO-Channels.html#g-io-channel-set-close-on-unref">g_io_channel_set_close_on_unref</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gboolean">gboolean</a> do_close);<a href="glib-IO-Channels.html#GIOError">GIOError</a> <a href="glib-IO-Channels.html#g-io-channel-read">g_io_channel_read</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gchar">gchar</a> *buf, <a href="glib-Basic-Types.html#gsize">gsize</a> count, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_read);enum <a href="glib-IO-Channels.html#GIOError">GIOError</a>;<a href="glib-IO-Channels.html#GIOError">GIOError</a> <a href="glib-IO-Channels.html#g-io-channel-write">g_io_channel_write</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, const <a href="glib-Basic-Types.html#gchar">gchar</a> *buf, <a href="glib-Basic-Types.html#gsize">gsize</a> count, <a href="glib-Basic-Types.html#gsize">gsize</a> *bytes_written);<a href="glib-IO-Channels.html#GIOError">GIOError</a> <a href="glib-IO-Channels.html#g-io-channel-seek">g_io_channel_seek</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel, <a href="glib-Basic-Types.html#gint64">gint64</a> offset, <a href="glib-IO-Channels.html#GSeekType">GSeekType</a> type);void <a href="glib-IO-Channels.html#g-io-channel-close">g_io_channel_close</a> (<a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a> *channel);</pre></div><div class="refsect1" lang="en"><a name="id2908513"></a><h2>Description</h2><p>The <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> data type aims to provide a portable method for using filedescriptors, pipes, and sockets, and integrating them into the<a href="glib-The-Main-Event-Loop.html" title="The Main Event Loop">main event loop</a>.Currently full support is available on UNIX platforms, support forWindows is only partially complete.</p><p>To create a new <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> on UNIX systems use <a href="glib-IO-Channels.html#g-io-channel-unix-new"><code class="function">g_io_channel_unix_new()</code></a>.This works for plain file descriptors, pipes and sockets.Alternatively, a channel can be created for a file in a system independentmanner using <a href="glib-IO-Channels.html#g-io-channel-new-file"><code class="function">g_io_channel_new_file()</code></a>.</p><p>Once a <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> has been created, it can be used in a generic mannerwith the functions <a href="glib-IO-Channels.html#g-io-channel-read-chars"><code class="function">g_io_channel_read_chars()</code></a>, <a href="glib-IO-Channels.html#g-io-channel-write-chars"><code class="function">g_io_channel_write_chars()</code></a>,<a href="glib-IO-Channels.html#g-io-channel-seek-position"><code class="function">g_io_channel_seek_position()</code></a>, and <a href="glib-IO-Channels.html#g-io-channel-close"><code class="function">g_io_channel_close()</code></a>.</p><p>To add a <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> to the <a href="glib-The-Main-Event-Loop.html" title="The Main Event Loop">main event loop</a>use <a href="glib-IO-Channels.html#g-io-add-watch"><code class="function">g_io_add_watch()</code></a> or <a href="glib-IO-Channels.html#g-io-add-watch-full"><code class="function">g_io_add_watch_full()</code></a>. Here you specify which eventsyou are interested in on the <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a>, and provide a function to becalled whenever these events occur.</p><p><a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> instances are created with an initial reference count of 1.<a href="glib-IO-Channels.html#g-io-channel-ref"><code class="function">g_io_channel_ref()</code></a> and <a href="glib-IO-Channels.html#g-io-channel-unref"><code class="function">g_io_channel_unref()</code></a> can be used to increment ordecrement the reference count respectively. When the reference count fallsto 0, the <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> is freed. (Though it isn't closed automatically,unless it was created using <code class="function">g_io_channel_new_from_file()</code>.)Using <a href="glib-IO-Channels.html#g-io-add-watch"><code class="function">g_io_add_watch()</code></a> or <a href="glib-IO-Channels.html#g-io-add-watch-full"><code class="function">g_io_add_watch_full()</code></a> increments a channel'sreference count.</p><p>The new functions <a href="glib-IO-Channels.html#g-io-channel-read-chars"><code class="function">g_io_channel_read_chars()</code></a>, <a href="glib-IO-Channels.html#g-io-channel-read-line"><code class="function">g_io_channel_read_line()</code></a>,<a href="glib-IO-Channels.html#g-io-channel-read-line-string"><code class="function">g_io_channel_read_line_string()</code></a>, <a href="glib-IO-Channels.html#g-io-channel-read-to-end"><code class="function">g_io_channel_read_to_end()</code></a>,<a href="glib-IO-Channels.html#g-io-channel-write-chars"><code class="function">g_io_channel_write_chars()</code></a>, <a href="glib-IO-Channels.html#g-io-channel-seek-position"><code class="function">g_io_channel_seek_position()</code></a>,and <a href="glib-IO-Channels.html#g-io-channel-flush"><code class="function">g_io_channel_flush()</code></a> should not be mixed with thedeprecated functions <a href="glib-IO-Channels.html#g-io-channel-read"><code class="function">g_io_channel_read()</code></a>, <a href="glib-IO-Channels.html#g-io-channel-write"><code class="function">g_io_channel_write()</code></a>,and <a href="glib-IO-Channels.html#g-io-channel-seek"><code class="function">g_io_channel_seek()</code></a> on the same channel.</p></div><div class="refsect1" lang="en"><a name="id2908877"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2908883"></a><h3><a name="GIOChannel"></a>GIOChannel</h3><a class="indexterm" name="id2908893"></a><pre class="programlisting">typedef struct {} GIOChannel;</pre><p>A data structure representing an IO Channel. The fields should be consideredprivate and should only be accessed with the following functions.</p></div><hr><div class="refsect2" lang="en"><a name="id2908911"></a><h3><a name="g-io-channel-unix-new"></a>g_io_channel_unix_new ()</h3><a class="indexterm" name="id2908922"></a><pre class="programlisting"><a href="glib-IO-Channels.html#GIOChannel">GIOChannel</a>* g_io_channel_unix_new (int fd);</pre><p>Creates a new <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> given a file descriptor.On UNIX systems this works for plain files, pipes, and sockets.</p><p>The returned <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> has a reference count of 1.</p><p>The default encoding for <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a> is UTF-8. If your applicationis reading output from a command using via pipe, you may need toset the encoding to the encoding of the current locale (see<a href="glib-Character-Set-Conversion.html#g-get-charset"><code class="function">g_get_charset()</code></a>) with the <a href="glib-IO-Channels.html#g-io-channel-set-encoding"><code class="function">g_io_channel_set_encoding()</code></a> function.</p><p>If you want to read raw binary data without interpretation, thencall the <a href="glib-IO-Channels.html#g-io-channel-set-encoding"><code class="function">g_io_channel_set_encoding()</code></a> function with <code class="literal">NULL</code> for theencoding argument.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>fd</code></em> :</span></td><td>a file descriptor.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a new <a href="glib-IO-Channels.html#GIOChannel"><span class="type">GIOChannel</span></a>.</td></tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -