#include <gromacs/utility/file.h>
Basic file object.
This class provides basic file I/O functionality and uses exceptions (FileIOError) for error reporting.
|
static FILE * | openRawHandle (const char *filename, const char *mode) |
| Opens a file and returns a FILE handle. More...
|
|
static FILE * | openRawHandle (const std::string &filename, const char *mode) |
| Opens a file and returns a FILE handle. More...
|
|
static bool | exists (const char *filename) |
| Checks whether a file exists and is a regular file. More...
|
|
static bool | exists (const std::string &filename) |
| Checks whether a file exists and is a regular file. More...
|
|
static File & | standardInput () |
| Returns a File object for accessing stdin. More...
|
|
static File & | standardOutput () |
| Returns a File object for accessing stdout. More...
|
|
static File & | standardError () |
| Returns a File object for accessing stderr. More...
|
|
static std::string | readToString (const char *filename) |
| Reads contents of a file to a std::string. More...
|
|
static std::string | readToString (const std::string &filename) |
| Reads contents of a file to a std::string. More...
|
|
static void | writeFileFromString (const std::string &filename, const std::string &text) |
| Convenience method for writing a file from a string in a single call. More...
|
|
gmx::File::File |
( |
const char * |
filename, |
|
|
const char * |
mode |
|
) |
| |
Creates a file object and opens a file.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
- See Also
- open(const char *, const char *)
gmx::File::File |
( |
const std::string & |
filename, |
|
|
const char * |
mode |
|
) |
| |
Creates a file object and opens a file.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
- See Also
- open(const char *, const char *)
Creates a file object and opens a file.
- Parameters
-
[in] | initializer | Parameters to open the file. |
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
Destroys the file object.
If the file is still open, it is closed. Any error conditions will be ignored.
void gmx::File::close |
( |
| ) |
|
Closes the file object.
- Exceptions
-
The file must be open.
bool gmx::File::exists |
( |
const char * |
filename | ) |
|
|
static |
Checks whether a file exists and is a regular file.
- Parameters
-
[in] | filename | Path to the file to check. |
- Returns
- true if
filename
exists and is accessible.
Does not throw.
bool gmx::File::exists |
( |
const std::string & |
filename | ) |
|
|
static |
Checks whether a file exists and is a regular file.
- Parameters
-
[in] | filename | Path to the file to check. |
- Returns
- true if
filename
exists and is accessible.
Does not throw.
FILE * gmx::File::handle |
( |
| ) |
|
Returns a file handle for interfacing with C functions.
The file must be open. Does not throw.
bool gmx::File::isInteractive |
( |
| ) |
const |
Returns whether the file is an interactive terminal.
Only works on Unix, otherwise always returns true. It only makes sense to call this for File::standardInput() and friends.
Thie file must be open. Does not throw.
void gmx::File::open |
( |
const char * |
filename, |
|
|
const char * |
mode |
|
) |
| |
Opens a file.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
The file object must not be open.
void gmx::File::open |
( |
const std::string & |
filename, |
|
|
const char * |
mode |
|
) |
| |
Opens a file.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
The file object must not be open.
FILE * gmx::File::openRawHandle |
( |
const char * |
filename, |
|
|
const char * |
mode |
|
) |
| |
|
static |
Opens a file and returns a FILE
handle.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
Instead of returning NULL
on errors, throws an exception with additional details (including the file name and errno
).
FILE * gmx::File::openRawHandle |
( |
const std::string & |
filename, |
|
|
const char * |
mode |
|
) |
| |
|
static |
Opens a file and returns a FILE
handle.
- Parameters
-
[in] | filename | Path of the file to open. |
[in] | mode | Mode to open the file in (for fopen()). |
- Exceptions
-
Instead of returning NULL
on errors, throws an exception with additional details (including the file name and errno
).
void gmx::File::readBytes |
( |
void * |
buffer, |
|
|
size_t |
bytes |
|
) |
| |
Reads given number of bytes from the file.
- Parameters
-
[out] | buffer | Pointer to buffer that receives the bytes. |
[in] | bytes | Number of bytes to read. |
- Exceptions
-
The file must be open.
bool gmx::File::readLine |
( |
std::string * |
line | ) |
|
Reads a single line from the file.
- Parameters
-
[out] | line | String to receive the line. |
- Returns
- false if nothing was read because the file ended.
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
On error or when false is returned, line
will be empty. Trailing space will be removed from the line. To loop over all lines in the file, use:
std::string line;
while (file.readLine(&line))
{
}
bool gmx::File::readLineWithTrailingSpace |
( |
std::string * |
line | ) |
|
Reads a single line from the file.
- Parameters
-
[out] | line | String to receive the line. |
- Returns
- false if nothing was read because the file ended.
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
On error or when false is returned, line
will be empty. Works as readLine(), except that terminating newline will be present in line
if it was present in the file.
- See Also
- readLine()
std::string gmx::File::readToString |
( |
const char * |
filename | ) |
|
|
static |
Reads contents of a file to a std::string.
- Parameters
-
[in] | filename | Name of the file to read. |
- Returns
- The contents of
filename
.
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
std::string gmx::File::readToString |
( |
const std::string & |
filename | ) |
|
|
static |
Reads contents of a file to a std::string.
- Parameters
-
[in] | filename | Name of the file to read. |
- Returns
- The contents of
filename
.
- Exceptions
-
std::bad_alloc | if out of memory. |
FileIOError | on any I/O error. |
File & gmx::File::standardError |
( |
| ) |
|
|
static |
Returns a File object for accessing stderr.
- Exceptions
-
std::bad_alloc | if out of memory (only on first call). |
File & gmx::File::standardInput |
( |
| ) |
|
|
static |
Returns a File object for accessing stdin.
- Exceptions
-
std::bad_alloc | if out of memory (only on first call). |
File & gmx::File::standardOutput |
( |
| ) |
|
|
static |
Returns a File object for accessing stdout.
- Exceptions
-
std::bad_alloc | if out of memory (only on first call). |
void gmx::File::writeFileFromString |
( |
const std::string & |
filename, |
|
|
const std::string & |
text |
|
) |
| |
|
static |
Convenience method for writing a file from a string in a single call.
- Parameters
-
[in] | filename | Name of the file to read. |
[in] | text | String to write to filename . |
- Exceptions
-
If filename
exists, it is overwritten.
void gmx::File::writeLine |
( |
const char * |
line | ) |
|
Writes a line to the file.
- Parameters
-
- Exceptions
-
If line
does not end in a newline, one newline is appended. Otherwise, works as writeString().
The file must be open.
void gmx::File::writeLine |
( |
const std::string & |
line | ) |
|
|
inline |
Writes a line to the file.
- Parameters
-
- Exceptions
-
If line
does not end in a newline, one newline is appended. Otherwise, works as writeString().
The file must be open.
void gmx::File::writeLine |
( |
| ) |
|
Writes a newline to the file.
- Exceptions
-
void gmx::File::writeString |
( |
const char * |
str | ) |
|
Writes a string to the file.
- Parameters
-
- Exceptions
-
The file must be open.
void gmx::File::writeString |
( |
const std::string & |
str | ) |
|
|
inline |
Writes a string to the file.
- Parameters
-
- Exceptions
-
The file must be open.
The documentation for this class was generated from the following files:
- src/gromacs/utility/file.h
- src/gromacs/utility/file.cpp