Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QFile Class

TheQFile class provides an interface for reading from and writing to files.More...

Header:#include <QFile>
Inherits:QIODevice
Inherited By:

QTemporaryFile

Note: All functions in this class arereentrant, except forsetEncodingFunction() andsetDecodingFunction(), which are nonreentrant.

Public Types

typedefDecoderFn
typedefEncoderFn
enumFileError { NoError, ReadError, WriteError, FatalError, ..., CopyError }
enumFileHandleFlag { AutoCloseHandle, DontCloseHandle }
flagsFileHandleFlags
enumMemoryMapFlags { NoOptions }
enumPermission { ReadOwner, WriteOwner, ExeOwner, ReadUser, ..., ExeOther }
typedefPermissionSpec
flagsPermissions

Public Functions

QFile(const QString & name)
QFile(QObject * parent)
QFile(const QString & name, QObject * parent)
~QFile()
boolcopy(const QString & newName)
FileErrorerror() const
boolexists() const
QStringfileName() const
boolflush()
inthandle() const
boollink(const QString & linkName)
uchar *map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions)
boolopen(FILE * fh, OpenMode mode)
boolopen(FILE * fh, OpenMode mode, FileHandleFlags handleFlags)
boolopen(int fd, OpenMode mode)
boolopen(int fd, OpenMode mode, FileHandleFlags handleFlags)
boolopen(const RFile & f, OpenMode mode, FileHandleFlags handleFlags = DontCloseHandle)
Permissionspermissions() const
boolremove()
boolrename(const QString & newName)
boolresize(qint64 sz)
voidsetFileName(const QString & name)
boolsetPermissions(Permissions permissions)
QStringsymLinkTarget() const
boolunmap(uchar * address)
voidunsetError()

Reimplemented Public Functions

virtual boolatEnd() const
virtual voidclose()
virtual boolisSequential() const
virtual boolopen(OpenMode mode)
virtual qint64pos() const
virtual boolseek(qint64 pos)
virtual qint64size() const
  • 33 public functions inherited fromQIODevice
  • 29 public functions inherited fromQObject

Static Public Members

boolcopy(const QString & fileName, const QString & newName)
QStringdecodeName(const QByteArray & localFileName)
QStringdecodeName(const char * localFileName)
QByteArrayencodeName(const QString & fileName)
boolexists(const QString & fileName)
boollink(const QString & fileName, const QString & linkName)
Permissionspermissions(const QString & fileName)
boolremove(const QString & fileName)
boolrename(const QString & oldName, const QString & newName)
boolresize(const QString & fileName, qint64 sz)
voidsetDecodingFunction(DecoderFn function)
voidsetEncodingFunction(EncoderFn function)
boolsetPermissions(const QString & fileName, Permissions permissions)
QStringsymLinkTarget(const QString & fileName)
  • 7 static public members inherited fromQObject

Reimplemented Protected Functions

virtual qint64readData(char * data, qint64 len)
virtual qint64readLineData(char * data, qint64 maxlen)
virtual qint64writeData(const char * data, qint64 len)
  • 5 protected functions inherited fromQIODevice
  • 8 protected functions inherited fromQObject

Additional Inherited Members

Detailed Description

TheQFile class provides an interface for reading from and writing to files.

QFile is an I/O device for reading and writing text and binary files andresources. AQFile may be used by itself or, more conveniently, with aQTextStream orQDataStream.

The file name is usually passed in the constructor, but it can be set at any time usingsetFileName().QFile expects the file separator to be '/' regardless of operating system. The use of other separators (e.g., '\') is not supported.

You can check for a file's existence usingexists(), and remove a file usingremove(). (More advanced file system related operations are provided byQFileInfo andQDir.)

The file is opened withopen(), closed withclose(), and flushed withflush(). Data is usually read and written usingQDataStream orQTextStream, but you can also call theQIODevice-inherited functionsread(),readLine(),readAll(),write().QFile also inheritsgetChar(),putChar(), andungetChar(), which work one character at a time.

The size of the file is returned bysize(). You can get the current file position usingpos(), or move to a new file position usingseek(). If you've reached the end of the file,atEnd() returns true.

Reading Files Directly

The following example reads a text file line by line:

QFile file("in.txt");if (!file.open(QIODevice::ReadOnly|QIODevice::Text))return;while (!file.atEnd()) {QByteArray line= file.readLine();        process_line(line);    }

TheQIODevice::Text flag passed toopen() tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n"). By default,QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.

Using Streams to Read Files

The next example usesQTextStream to read a text file line by line:

QFile file("in.txt");if (!file.open(QIODevice::ReadOnly|QIODevice::Text))return;QTextStream in(&file);while (!in.atEnd()) {QString line= in.readLine();        process_line(line);    }

QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit UnicodeQString. By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; seeQTextCodec::codecForLocale() for details). This can be changed using setCodec().

To write text, we can use operator<<(), which is overloaded to take aQTextStream on the left and various data types (includingQString) on the right:

QFile file("out.txt");if (!file.open(QIODevice::WriteOnly|QIODevice::Text))return;QTextStream out(&file);    out<<"The magic number is: "<<49<<"\n";

QDataStream is similar, in that you can use operator<<() to write data and operator>>() to read it back. See the class documentation for details.

When you useQFile,QFileInfo, andQDir to access the file system with Qt, you can use Unicode file names. On Unix, these file names are converted to an 8-bit encoding. If you want to use standard C++ APIs (<cstdio> or<iostream>) or platform-specific APIs to access files instead ofQFile, you can use theencodeName() anddecodeName() functions to convert between Unicode file names and 8-bit file names.

On Unix, there are some special system files (e.g. in/proc) for whichsize() will always return 0, yet you may still be able to read more data from such a file; the data is generated in direct response to you callingread(). In this case, however, you cannot useatEnd() to determine if there is more data to read (sinceatEnd() will return true for a file that claims to have size 0). Instead, you should either callreadAll(), or callread() orreadLine() repeatedly until no more data can be read. The next example usesQTextStream to read/proc/modules line by line:

QFile file("/proc/modules");if (!file.open(QIODevice::ReadOnly|QIODevice::Text))return;QTextStream in(&file);QString line= in.readLine();while (!line.isNull()) {        process_line(line);        line= in.readLine();    }

Signals

Unlike otherQIODevice implementations, such asQTcpSocket,QFile does not emit theaboutToClose(),bytesWritten(), orreadyRead() signals. This implementation detail means thatQFile is not suitable for reading and writing certain types of files, such as device files on Unix platforms.

Platform Specific Issues

File permissions are handled differently on Linux/Mac OS X and Windows. In a nonwritable directory on Linux, files cannot be created. This is not always the case on Windows, where, for instance, the 'My Documents' directory usually is not writable, but it is still possible to create files in it.

See alsoQTextStream,QDataStream,QFileInfo,QDir, andThe Qt Resource System.

Member Type Documentation

typedef QFile::DecoderFn

This is a typedef for a pointer to a function with the following signature:

QString myDecoderFunc(constQByteArray&localFileName);

See alsosetDecodingFunction().

typedef QFile::EncoderFn

This is a typedef for a pointer to a function with the following signature:

QByteArray myEncoderFunc(constQString&fileName);

See alsosetEncodingFunction() andencodeName().

enum QFile::FileError

This enum describes the errors that may be returned by theerror() function.

ConstantValueDescription
QFile::NoError0No error occurred.
QFile::ReadError1An error occurred when reading from the file.
QFile::WriteError2An error occurred when writing to the file.
QFile::FatalError3A fatal error occurred.
QFile::ResourceError4 
QFile::OpenError5The file could not be opened.
QFile::AbortError6The operation was aborted.
QFile::TimeOutError7A timeout occurred.
QFile::UnspecifiedError8An unspecified error occurred.
QFile::RemoveError9The file could not be removed.
QFile::RenameError10The file could not be renamed.
QFile::PositionError11The position in the file could not be changed.
QFile::ResizeError12The file could not be resized.
QFile::PermissionsError13The file could not be accessed.
QFile::CopyError14The file could not be copied.

enum QFile::FileHandleFlag
flags QFile::FileHandleFlags

This enum is used when opening a file to specify additional options which only apply to files and not to a genericQIODevice.

ConstantValueDescription
QFile::AutoCloseHandle0x0001The file handle passed intoopen() should be closed byclose(), the default behaviour is that close just flushes the file and the application is responsible for closing the file handle. When opening a file by name, this flag is ignored as Qt always "owns" the file handle and must close it.
QFile::DontCloseHandle0The file handle passed intoopen() will not be closed by Qt. The application must ensure thatclose() is called.

This enum was introduced or modified in Qt 4.8.

The FileHandleFlags type is a typedef forQFlags<FileHandleFlag>. It stores an OR combination of FileHandleFlag values.

enum QFile::MemoryMapFlags

This enum describes special options that may be used by themap() function.

ConstantValueDescription
QFile::NoOptions0No options.

This enum was introduced or modified in Qt 4.4.

enum QFile::Permission
flags QFile::Permissions

This enum is used by the permission() function to report the permissions and ownership of a file. The values may be OR-ed together to test multiple permissions and ownership values.

ConstantValueDescription
QFile::ReadOwner0x4000The file is readable by the owner of the file.
QFile::WriteOwner0x2000The file is writable by the owner of the file.
QFile::ExeOwner0x1000The file is executable by the owner of the file.
QFile::ReadUser0x0400The file is readable by the user.
QFile::WriteUser0x0200The file is writable by the user.
QFile::ExeUser0x0100The file is executable by the user.
QFile::ReadGroup0x0040The file is readable by the group.
QFile::WriteGroup0x0020The file is writable by the group.
QFile::ExeGroup0x0010The file is executable by the group.
QFile::ReadOther0x0004The file is readable by anyone.
QFile::WriteOther0x0002The file is writable by anyone.
QFile::ExeOther0x0001The file is executable by anyone.

Warning: Because of differences in the platforms supported by Qt, the semantics of ReadUser, WriteUser and ExeUser are platform-dependent: On Unix, the rights of the owner of the file are returned and on Windows the rights of the current user are returned. This behavior might change in a future Qt version.

Note that Qt does not by default check for permissions on NTFS file systems, as this may decrease the performance of file handling considerably. It is possible to force permission checking on NTFS by including the following code in your source:

extern Q_CORE_EXPORTint qt_ntfs_permission_lookup;

Permission checking is then turned on and off by incrementing and decrementingqt_ntfs_permission_lookup by 1.

qt_ntfs_permission_lookup++;// turn checking onqt_ntfs_permission_lookup--;// turn it off again

The Permissions type is a typedef forQFlags<Permission>. It stores an OR combination of Permission values.

typedef QFile::PermissionSpec

UseQFile::Permission instead.

Member Function Documentation

QFile::QFile(constQString & name)

Constructs a new file object to represent the file with the givenname.

QFile::QFile(QObject * parent)

Constructs a new file object with the givenparent.

QFile::QFile(constQString & name,QObject * parent)

Constructs a new file object with the givenparent to represent the file with the specifiedname.

QFile::~QFile()

Destroys the file object, closing it if necessary.

[virtual]bool QFile::atEnd() const

Reimplemented fromQIODevice::atEnd().

Returns true if the end of the file has been reached; otherwise returns false.

For regular empty files on Unix (e.g. those in/proc), this function returns true, since the file system reports that the size of such a file is 0. Therefore, you should not depend on atEnd() when reading data from such a file, but rather callread() until no more data can be read.

[virtual]void QFile::close()

Reimplemented fromQIODevice::close().

CallsQFile::flush() and closes the file. Errors from flush are ignored.

See alsoQIODevice::close().

bool QFile::copy(constQString & newName)

Copies the file currently specified byfileName() to a file callednewName. Returns true if successful; otherwise returns false.

Note that if a file with the namenewName already exists, copy() returns false (i.e.QFile will not overwrite it).

The source file is closed before it is copied.

See alsosetFileName().

[static]bool QFile::copy(constQString & fileName, constQString & newName)

This is an overloaded function.

Copies the filefileName tonewName. Returns true if successful; otherwise returns false.

If a file with the namenewName already exists,copy() returns false (i.e.,QFile will not overwrite it).

See alsorename().

[static]QString QFile::decodeName(constQByteArray & localFileName)

This does the reverse ofQFile::encodeName() usinglocalFileName.

See alsosetDecodingFunction() andencodeName().

[static]QString QFile::decodeName(constchar * localFileName)

This is an overloaded function.

Returns the Unicode version of the givenlocalFileName. SeeencodeName() for details.

[static]QByteArray QFile::encodeName(constQString & fileName)

By default, this function convertsfileName to the local 8-bit encoding determined by the user's locale. This is sufficient for file names that the user chooses. File names hard-coded into the application should only use 7-bit ASCII filename characters.

See alsodecodeName() andsetEncodingFunction().

FileError QFile::error() const

Returns the file error status.

The I/O device status returns an error code. For example, ifopen() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.

See alsounsetError().

[static]bool QFile::exists(constQString & fileName)

Returns true if the file specified byfileName exists; otherwise returns false.

bool QFile::exists() const

This is an overloaded function.

Returns true if the file specified byfileName() exists; otherwise returns false.

See alsofileName() andsetFileName().

QString QFile::fileName() const

Returns the name set bysetFileName() or to theQFile constructors.

See alsosetFileName() andQFileInfo::fileName().

bool QFile::flush()

Flushes any buffered data to the file. Returns true if successful; otherwise returns false.

int QFile::handle() const

Returns the file handle of the file.

This is a small positive integer, suitable for use with C library functions such as fdopen() and fcntl(). On systems that use file descriptors for sockets (i.e. Unix systems, but not Windows) the handle can be used withQSocketNotifier as well.

If the file is not open, or there is an error, handle() returns -1.

This function is not supported on Windows CE.

On Symbian, this function returns -1 if the file was opened normally, as Symbian OS native file handles do not fit in an int, and are incompatible with C library functions that the handle would be used for. If the file was opened using the overloads that take an open C library file handle / file descriptor, then this function returns that same handle.

See alsoQSocketNotifier.

[virtual]bool QFile::isSequential() const

Reimplemented fromQIODevice::isSequential().

Returns true if the file can only be manipulated sequentially; otherwise returns false.

Most files support random-access, but some special files may not.

See alsoQIODevice::isSequential().

bool QFile::link(constQString & linkName)

Creates a link namedlinkName that points to the file currently specified byfileName(). What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.

This function will not overwrite an already existing entity in the file system; in this case,link() will return false and seterror() to returnRenameError.

Note:To create a valid link on Windows,linkName must have a.lnk file extension.

Note:Symbian filesystem does not support links.

See alsosetFileName().

[static]bool QFile::link(constQString & fileName, constQString & linkName)

This is an overloaded function.

Creates a link namedlinkName that points to the filefileName. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.

See alsolink().

uchar * QFile::map(qint64 offset,qint64 size,MemoryMapFlags flags = NoOptions)

Mapssize bytes of the file into memory starting atoffset. A file should be open for a map to succeed but the file does not need to stay open after the memory has been mapped. When theQFile is destroyed or a new file is opened with this object, any maps that have not been unmapped will automatically be unmapped.

Any mapping options can be passed throughflags.

Returns a pointer to the memory or 0 if there is an error.

Note:On Windows CE 5.0 the file will be closed before mapping occurs.

This function was introduced in Qt 4.4.

See alsounmap() andQAbstractFileEngine::supportsExtension().

[virtual]bool QFile::open(OpenMode mode)

Reimplemented fromQIODevice::open().

Opens the file usingOpenModemode, returning true if successful; otherwise false.

Themode must beQIODevice::ReadOnly,QIODevice::WriteOnly, orQIODevice::ReadWrite. It may also have additional flags, such asQIODevice::Text andQIODevice::Unbuffered.

Note:InWriteOnly orReadWrite mode, if the relevant file does not already exist, this function will try to create a new file before opening it.

See alsoQIODevice::OpenMode andsetFileName().

bool QFile::open(FILE * fh,OpenMode mode)

This is an overloaded function.

Opens the existing file handlefh in the givenmode. Returns true if successful; otherwise returns false.

Example:

#include <stdio.h>void printError(constchar* msg){QFile file;    file.open(stderr,QIODevice::WriteOnly);    file.write(msg, qstrlen(msg));// write to stderr    file.close();}

When aQFile is opened using this function,close() does not actually close the file, but only flushes it.

Warning:

  1. Iffh does not refer to a regular file, e.g., it isstdin,stdout, orstderr, you may not be able toseek().size() returns0 in those cases. SeeQIODevice::isSequential() for more information.
  2. Since this function opens the file without specifying the file name, you cannot use thisQFile with aQFileInfo.

Note:For Windows CE you may not be able to callresize().

Note for the Windows Platform

fh must be opened in binary mode (i.e., the mode string must contain 'b', as in "rb" or "wb") when accessing files and other random-access devices. Qt will translate the end-of-line characters if you passQIODevice::Text tomode. Sequential devices, such as stdin and stdout, are unaffected by this limitation.

You need to enable support for console applications in order to use the stdin, stdout and stderr streams at the console. To do this, add the following declaration to your application's project file:

CONFIG+= console

See alsoclose() andqmake Variable Reference.

bool QFile::open(FILE * fh,OpenMode mode,FileHandleFlags handleFlags)

This is an overloaded function.

Opens the existing file handlefh in the givenmode. Returns true if successful; otherwise returns false.

Example:

#include <stdio.h>void printError(constchar* msg){QFile file;    file.open(stderr,QIODevice::WriteOnly);    file.write(msg, qstrlen(msg));// write to stderr    file.close();}

When aQFile is opened using this function, behaviour ofclose() is controlled by theAutoCloseHandle flag. IfAutoCloseHandle is specified, and this function succeeds, then callingclose() closes the adopted handle. Otherwise,close() does not actually close the file, but only flushes it.

Warning:

  1. Iffh does not refer to a regular file, e.g., it isstdin,stdout, orstderr, you may not be able toseek().size() returns0 in those cases. SeeQIODevice::isSequential() for more information.
  2. Since this function opens the file without specifying the file name, you cannot use thisQFile with aQFileInfo.

Note:For Windows CE you may not be able to callresize().

Note for the Windows Platform

fh must be opened in binary mode (i.e., the mode string must contain 'b', as in "rb" or "wb") when accessing files and other random-access devices. Qt will translate the end-of-line characters if you passQIODevice::Text tomode. Sequential devices, such as stdin and stdout, are unaffected by this limitation.

You need to enable support for console applications in order to use the stdin, stdout and stderr streams at the console. To do this, add the following declaration to your application's project file:

CONFIG+= console

See alsoclose() andqmake Variable Reference.

bool QFile::open(int fd,OpenMode mode)

This is an overloaded function.

Opens the existing file descriptorfd in the givenmode. Returns true if successful; otherwise returns false.

When aQFile is opened using this function,close() does not actually close the file.

TheQFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.

Warning: Iffd is not a regular file, e.g, it is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able toseek(). In those cases,size() returns0. SeeQIODevice::isSequential() for more information.

Warning: For Windows CE you may not be able to callseek(), setSize(), fileTime().size() returns0.

Warning: Since this function opens the file without specifying the file name, you cannot use thisQFile with aQFileInfo.

See alsoclose().

bool QFile::open(int fd,OpenMode mode,FileHandleFlags handleFlags)

This is an overloaded function.

Opens the existing file descriptorfd in the givenmode. Returns true if successful; otherwise returns false.

When aQFile is opened using this function, behaviour ofclose() is controlled by thehandleFlags argument. IfAutoCloseHandle is specified, and this function succeeds, then callingclose() closes the adopted handle. Otherwise,close() does not actually close the file, but only flushes it.

TheQFile that is opened using this function is automatically set to be in raw mode; this means that the file input/output functions are slow. If you run into performance issues, you should try to use one of the other open functions.

Warning: Iffd is not a regular file, e.g, it is 0 (stdin), 1 (stdout), or 2 (stderr), you may not be able toseek(). In those cases,size() returns0. SeeQIODevice::isSequential() for more information.

Warning: For Windows CE you may not be able to callseek(), setSize(), fileTime().size() returns0.

Warning: Since this function opens the file without specifying the file name, you cannot use thisQFile with aQFileInfo.

See alsoclose().

bool QFile::open(constRFile & f,OpenMode mode,FileHandleFlags handleFlags = DontCloseHandle)

This is an overloaded function.

Opens the existing file objectf in the givenmode. Returns true if successful; otherwise returns false.

When aQFile is opened using this function, behaviour ofclose() is controlled by thehandleFlags argument. IfAutoCloseHandle is specified, and this function succeeds, then callingclose() closes the adopted handle. Otherwise,close() does not actually close the file, but only flushes it.

Warning: If the file handle is adopted from another process, you may not be able to use thisQFile with aQFileInfo.

See alsoclose().

Permissions QFile::permissions() const

Returns the complete OR-ed together combination ofQFile::Permission for the file.

See alsosetPermissions() andsetFileName().

[static]Permissions QFile::permissions(constQString & fileName)

This is an overloaded function.

Returns the complete OR-ed together combination ofQFile::Permission forfileName.

[virtual]qint64 QFile::pos() const

Reimplemented fromQIODevice::pos().

[virtual protected]qint64 QFile::readData(char * data,qint64 len)

Reimplemented fromQIODevice::readData().

[virtual protected]qint64 QFile::readLineData(char * data,qint64 maxlen)

Reimplemented fromQIODevice::readLineData().

bool QFile::remove()

Removes the file specified byfileName(). Returns true if successful; otherwise returns false.

The file is closed before it is removed.

See alsosetFileName().

[static]bool QFile::remove(constQString & fileName)

This is an overloaded function.

Removes the file specified by thefileName given.

Returns true if successful; otherwise returns false.

See alsoremove().

bool QFile::rename(constQString & newName)

Renames the file currently specified byfileName() tonewName. Returns true if successful; otherwise returns false.

If a file with the namenewName already exists, rename() returns false (i.e.,QFile will not overwrite it).

The file is closed before it is renamed.

See alsosetFileName().

[static]bool QFile::rename(constQString & oldName, constQString & newName)

This is an overloaded function.

Renames the fileoldName tonewName. Returns true if successful; otherwise returns false.

If a file with the namenewName already exists,rename() returns false (i.e.,QFile will not overwrite it).

See alsorename().

bool QFile::resize(qint64 sz)

Sets the file size (in bytes)sz. Returns true if the file if the resize succeeds; false otherwise. Ifsz is larger than the file currently is the new bytes will be set to 0, ifsz is smaller the file is simply truncated.

See alsosize() andsetFileName().

[static]bool QFile::resize(constQString & fileName,qint64 sz)

This is an overloaded function.

SetsfileName to size (in bytes)sz. Returns true if the file if the resize succeeds; false otherwise. Ifsz is larger thanfileName currently is the new bytes will be set to 0, ifsz is smaller the file is simply truncated.

See alsoresize().

[virtual]bool QFile::seek(qint64 pos)

Reimplemented fromQIODevice::seek().

For random-access devices, this function sets the current position topos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

Seeking beyond the end of a file: If the position is beyond the end of a file, then seek() shall not immediately extend the file. If a write is performed at this position, then the file shall be extended. The content of the file between the previous end of file and the newly written data is UNDEFINED and varies between platforms and file systems.

[static]void QFile::setDecodingFunction(DecoderFn function)

Sets thefunction for decoding 8-bit file names. The default uses the locale-specific 8-bit encoding.

Warning: This function is notreentrant.

See alsosetEncodingFunction() anddecodeName().

[static]void QFile::setEncodingFunction(EncoderFn function)

Sets thefunction for encoding Unicode file names. The default encodes in the locale-specific 8-bit encoding.

Warning: This function is notreentrant.

See alsoencodeName() andsetDecodingFunction().

void QFile::setFileName(constQString & name)

Sets thename of the file. The name can have no path, a relative path, or an absolute path.

Do not call this function if the file has already been opened.

If the file name has no path or a relative path, the path used will be the application's current directory pathat the time of theopen() call.

Example:

QFile file;QDir::setCurrent("/tmp");file.setFileName("readme.txt");QDir::setCurrent("/home");file.open(QIODevice::ReadOnly);// opens "/home/readme.txt" under Unix

Note that the directory separator "/" works for all operating systems supported by Qt.

See alsofileName(),QFileInfo, andQDir.

bool QFile::setPermissions(Permissions permissions)

Sets the permissions for the file to thepermissions specified. Returns true if successful, or false if the permissions cannot be modified.

See alsopermissions() andsetFileName().

[static]bool QFile::setPermissions(constQString & fileName,Permissions permissions)

This is an overloaded function.

Sets the permissions forfileName file topermissions.

[virtual]qint64 QFile::size() const

Reimplemented fromQIODevice::size().

Returns the size of the file.

For regular empty files on Unix (e.g. those in/proc), this function returns 0; the contents of such a file are generated on demand in response to you callingread().

[static]QString QFile::symLinkTarget(constQString & fileName)

Returns the absolute path of the file or directory referred to by the symlink (or shortcut on Windows) specified byfileName, or returns an empty string if thefileName does not correspond to a symbolic link.

This name may not represent an existing file; it is only a string.QFile::exists() returns true if the symlink points to an existing file.

This function was introduced in Qt 4.2.

QString QFile::symLinkTarget() const

This is an overloaded function.

Returns the absolute path of the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.

This name may not represent an existing file; it is only a string.QFile::exists() returns true if the symlink points to an existing file.

This function was introduced in Qt 4.2.

See alsofileName() andsetFileName().

bool QFile::unmap(uchar * address)

Unmaps the memoryaddress.

Returns true if the unmap succeeds; false otherwise.

This function was introduced in Qt 4.4.

See alsomap() andQAbstractFileEngine::supportsExtension().

void QFile::unsetError()

Sets the file's error toQFile::NoError.

See alsoerror().

[virtual protected]qint64 QFile::writeData(constchar * data,qint64 len)

Reimplemented fromQIODevice::writeData().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp