nsILocalFile:openNSPRFileDesc
From MDC
Contents |
[edit] Summary
This method is used for opening the file corresponding to this nsILocalFile.
[noscript] PRFileDescStar openNSPRFileDesc( in long aFlags, in long aMode );
[edit] Parameters
- aFlags
- [in] Bitwise combination of the following open flags:
- PR_RDONLY
- Open for reading only.
- PR_WRONLY
- Open for writing only.
- PR_RDWR
- Open for reading and writing.
- PR_CREATE_FILE
- If the file does not exist, the file is created. If the file exists, this flag has no effect.
- PR_APPEND
- The file pointer is set to the end of the file prior to each write.
- PR_TRUNCATE
- If the file exists, its length is truncated to 0.
- PR_SYNC
- If set, each write will wait for both the file data and file status to be physically updated.
- PR_EXCL
- With PR_CREATE_FILE, if the file does not exist, the file is created. If the file already exists, no action and null is returned.
- aMode
- [in] A UNIX-style file permissions value. For example, the octal value 0600 may be used to limit read and write access to the current user of the system. This parameter may be ignored on systems that do not support file permissions.
[edit] Return Values
PRFileDescStar is a typedef for a pointer to PRFileDesc.
[edit] Example Code
#include "prio.h"
#include "nsError.h"
#include "nsILocalFile.h"
// Read the contents of a nsILocalFile...
nsresult ReadLocalFile(nsILocalFile *aLocalFile)
{
PRFileDesc *fd;
nsresult rv = aLocalFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
if (NS_FAILED(rv))
return rv;
char buf[512];
PRInt32 n;
while ((n = PR_Read(fd, buf, sizeof(buf))) > 0)
{
// Do something with n-byte block of data from file...
}
if (n < 0)
rv = NS_ERROR_UNEXPECTED;
PR_Close(fd);
return rv;
}