📄 glib-file-utilities.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>File Utilities</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-utilities.html" title="GLib Utilities"><link rel="prev" href="glib-Spawning-Processes.html" title="Spawning Processes"><link rel="next" href="glib-Shell-related-Utilities.html" title="Shell-related Utilities"><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-Spawning-Processes.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-utilities.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-Shell-related-Utilities.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-File-Utilities"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">File Utilities</span></h2><p>File Utilities — various file-related functions.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h>#include <glib/gstdio.h>enum <a href="glib-File-Utilities.html#GFileError">GFileError</a>;#define <a href="glib-File-Utilities.html#G-FILE-ERROR:CAPS">G_FILE_ERROR</a>enum <a href="glib-File-Utilities.html#GFileTest">GFileTest</a>;<a href="glib-File-Utilities.html#GFileError">GFileError</a> <a href="glib-File-Utilities.html#g-file-error-from-errno">g_file_error_from_errno</a> (<a href="glib-Basic-Types.html#gint">gint</a> err_no);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-File-Utilities.html#g-file-get-contents">g_file_get_contents</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, <a href="glib-Basic-Types.html#gchar">gchar</a> **contents, <a href="glib-Basic-Types.html#gsize">gsize</a> *length, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gboolean">gboolean</a> <a href="glib-File-Utilities.html#g-file-test">g_file_test</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, <a href="glib-File-Utilities.html#GFileTest">GFileTest</a> test);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-File-Utilities.html#g-mkstemp">g_mkstemp</a> (<a href="glib-Basic-Types.html#gchar">gchar</a> *tmpl);<a href="glib-Basic-Types.html#gint">gint</a> <a href="glib-File-Utilities.html#g-file-open-tmp">g_file_open_tmp</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *tmpl, <a href="glib-Basic-Types.html#gchar">gchar</a> **name_used, <a href="glib-Error-Reporting.html#GError">GError</a> **error);<a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-File-Utilities.html#g-file-read-link">g_file_read_link</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, <a href="glib-Error-Reporting.html#GError">GError</a> **error); <a href="glib-File-Utilities.html#GDir">GDir</a>;<a href="glib-File-Utilities.html#GDir">GDir</a>* <a href="glib-File-Utilities.html#g-dir-open">g_dir_open</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *path, <a href="glib-Basic-Types.html#guint">guint</a> flags, <a href="glib-Error-Reporting.html#GError">GError</a> **error);const <a href="glib-Basic-Types.html#gchar">gchar</a>* <a href="glib-File-Utilities.html#g-dir-read-name">g_dir_read_name</a> (<a href="glib-File-Utilities.html#GDir">GDir</a> *dir);void <a href="glib-File-Utilities.html#g-dir-rewind">g_dir_rewind</a> (<a href="glib-File-Utilities.html#GDir">GDir</a> *dir);void <a href="glib-File-Utilities.html#g-dir-close">g_dir_close</a> (<a href="glib-File-Utilities.html#GDir">GDir</a> *dir);int <a href="glib-File-Utilities.html#g-open">g_open</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, int flags, int mode);int <a href="glib-File-Utilities.html#g-rename">g_rename</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *oldfilename, const <a href="glib-Basic-Types.html#gchar">gchar</a> *newfilename);int <a href="glib-File-Utilities.html#g-mkdir">g_mkdir</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, int mode);int <a href="glib-File-Utilities.html#g-stat">g_stat</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, struct stat *buf);int <a href="glib-File-Utilities.html#g-lstat">g_lstat</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, struct stat *buf);int <a href="glib-File-Utilities.html#g-unlink">g_unlink</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename);int <a href="glib-File-Utilities.html#g-remove">g_remove</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename);int <a href="glib-File-Utilities.html#g-rmdir">g_rmdir</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename);FILE* <a href="glib-File-Utilities.html#g-fopen">g_fopen</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, const <a href="glib-Basic-Types.html#gchar">gchar</a> *mode);FILE* <a href="glib-File-Utilities.html#g-freopen">g_freopen</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *filename, const <a href="glib-Basic-Types.html#gchar">gchar</a> *mode, FILE *stream);</pre></div><div class="refsect1" lang="en"><a name="id3052094"></a><h2>Description</h2><p>There is a group of functions which wrap the common POSIX functions dealing with filenames (<a href="glib-File-Utilities.html#g-open"><code class="function">g_open()</code></a>, <a href="glib-File-Utilities.html#g-rename"><code class="function">g_rename()</code></a>, <a href="glib-File-Utilities.html#g-mkdir"><code class="function">g_mkdir()</code></a>, <a href="glib-File-Utilities.html#g-stat"><code class="function">g_stat()</code></a>, <a href="glib-File-Utilities.html#g-unlink"><code class="function">g_unlink()</code></a>, <a href="glib-File-Utilities.html#g-remove"><code class="function">g_remove()</code></a>, <a href="glib-File-Utilities.html#g-fopen"><code class="function">g_fopen()</code></a>, <a href="glib-File-Utilities.html#g-freopen"><code class="function">g_freopen()</code></a>). The point of these wrappers is to make it possible to handle file names with any Unicode characters in them on Windows without having to use ifdefs and the wide character API in the application code.</p><p>The pathname argument should be in the GLib file name encoding. OnPOSIX this is the actual on-disk encoding which might correspond tothe locale settings of the process (or the <code class="envar">G_FILENAME_ENCODING</code> environment variable), or not.</p><p>On Windows the GLib file name encoding is UTF-8. Note that theMicrosoft C library does not use UTF-8, but has separate APIs forcurrent system code page and wide characters (UTF-16). The GLibwrappers call the wide character API if present (on modern Windowssystems), otherwise convert to/from the system code page.</p><p>Another group of functions allows to open and read directoriesin the GLib file name encoding. These are <a href="glib-File-Utilities.html#g-dir-open"><code class="function">g_dir_open()</code></a>, <a href="glib-File-Utilities.html#g-dir-read-name"><code class="function">g_dir_read_name()</code></a>, <a href="glib-File-Utilities.html#g-dir-rewind"><code class="function">g_dir_rewind()</code></a>, <a href="glib-File-Utilities.html#g-dir-close"><code class="function">g_dir_close()</code></a>.</p></div><div class="refsect1" lang="en"><a name="id3052255"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3052261"></a><h3><a name="GFileError"></a>enum GFileError</h3><a class="indexterm" name="id3052271"></a><pre class="programlisting">typedef enum{ G_FILE_ERROR_EXIST, G_FILE_ERROR_ISDIR, G_FILE_ERROR_ACCES, G_FILE_ERROR_NAMETOOLONG, G_FILE_ERROR_NOENT, G_FILE_ERROR_NOTDIR, G_FILE_ERROR_NXIO, G_FILE_ERROR_NODEV, G_FILE_ERROR_ROFS, G_FILE_ERROR_TXTBSY, G_FILE_ERROR_FAULT, G_FILE_ERROR_LOOP, G_FILE_ERROR_NOSPC, G_FILE_ERROR_NOMEM, G_FILE_ERROR_MFILE, G_FILE_ERROR_NFILE, G_FILE_ERROR_BADF, G_FILE_ERROR_INVAL, G_FILE_ERROR_PIPE, G_FILE_ERROR_AGAIN, G_FILE_ERROR_INTR, G_FILE_ERROR_IO, G_FILE_ERROR_PERM, G_FILE_ERROR_NOSYS, G_FILE_ERROR_FAILED} GFileError;</pre><p>Values corresponding to <code class="literal">errno</code> codes returned from file operationson UNIX. Unlike <code class="literal">errno</code> codes, <a href="glib-File-Utilities.html#GFileError"><span class="type">GFileError</span></a> values are available on all systems, even Windows. The exact meaning of each code depends on whatsort of file operation you were performing; the UNIX documentationgives more details. The following error code descriptions come from the GNU C Library manual, and are under the copyrightof that manual.</p><p>It's not very portable to make detailed assumptions about exactlywhich errors will be returned from a given operation. Some errorsdon't occur on some systems, etc., sometimes there are subtledifferences in when a system will report a given error, etc.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><code class="literal">G_FILE_ERROR_EXIST</code></span></td><td>Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_ISDIR</code></span></td><td>File is a directory; you cannot open a directory for writing, or create or remove hard links to it.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_ACCES</code></span></td><td>Permission denied; the file permissions do not allow the attempted operation.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NAMETOOLONG</code></span></td><td>Filename too long.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NOENT</code></span></td><td>No such file or directory. This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NOTDIR</code></span></td><td>A file that isn't a directory was specified when a directory is required.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NXIO</code></span></td><td>No such device or address. The system tried to use the device represented by a file you specified, and it couldn't find the device. This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NODEV</code></span></td><td>This file is of a type that doesn't support mapping.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_ROFS</code></span></td><td>The directory containing the new link can't be modified because it's on a read-only file system.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_TXTBSY</code></span></td><td>Text file busy.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_FAULT</code></span></td><td>You passed in a pointer to bad memory. (GLib won't reliably return this, don't pass in pointers to bad memory.)</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_LOOP</code></span></td><td>Too many levels of symbolic links were encountered in looking up a file name. This often indicates a cycle of symbolic links.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NOSPC</code></span></td><td>No space left on device; write operation on a file failed because the disk is full.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NOMEM</code></span></td><td>No memory available. The system cannot allocate more virtual memory because its capacity is full.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_MFILE</code></span></td><td>The current process has too many files open and can't open any more. Duplicate descriptors do count toward this limit.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_NFILE</code></span></td><td>There are too many distinct file openings in the entire system.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_BADF</code></span></td><td>Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa).</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_INVAL</code></span></td><td>Invalid argument. This is used to indicate various kinds of problems with passing the wrong argument to a library function.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_PIPE</code></span></td><td>Broken pipe; there is no process reading from the other end of a pipe. Every library function that returns this error code also generates a `SIGPIPE' signal; this signal terminates the program if not handled or blocked. Thus, your program will never actually see this code unless it has handled or blocked `SIGPIPE'.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_AGAIN</code></span></td><td>Resource temporarily unavailable; the call might work if you try again later.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_INTR</code></span></td><td>Interrupted function call; an asynchronous signal occurred and prevented completion of the call. When this happens, you should try the call again.</td></tr><tr><td><span class="term"><code class="literal">G_FILE_ERROR_IO</code></span></td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -