C++ filebuf Class
Example
Use a filebuf
object to create a file:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create a file
filebuf MyFileBuf;
MyFileBuf.open("filename.txt", ios_base::out);
// Write into the file
MyFileBuf.sputn("Hello World!", 12);
// Close the file
MyFileBuf.close();
}
Definition and Usage
The filebuf
class is used to read and write files. There is a filebuf
object used internally by the fstream
, ifstream
and ofstream
classes.
The filebuf
class is defined in the <fstream>
header file.
File Handling Functions
File handling functions open and close files.
open()
The open(filepath, mode)
method opens the file at the path specified by filepath. If a file is already open then this method has no effect. The mode parameter is a set of flags that indicate the way in which the file will be used. The following flags can be used in the mode parameter:
ios_base::in
- The file is open for reading.ios_base::out
- The file is open for writing.ios_base::binary
- File contents are treated as binary data instead of text.ios_base::ate
- The file is opened with the file pointer at the end of the file.ios_base::app
- New data is always written to the end of the file.ios_base::trunc
- The contents of the file are deleted as soon as the file is opened.
Flags can be combined using the |
operator. For example, to open a file for both reading and writing, use ios_base::in|ios_base::out
.
filebuf MyFileBuf;
MyFileBuf.open("filename.txt", ios_base::in|ios_base::out);
is_open()
The is_open()
method returns a boolean value, true if a file is open and false if there is no file open.
filebuf MyFileBuf;
cout << MyFileBuf.is_open(); << "\n"; // Displays 0 because the file is not open
MyFileBuf.open("filename.txt");
cout << MyFileBuf.is_open(); << "\n"; // Displays 1 because the file is open
close()
The close()
method closes a file. It is good to close a file when you are finished working with it to free up resources.
MyFileBuf.close();
File Pointer Functions
File pointers are internal variables which indicate where in the file to read or write.
File pointer functions are used to manipulate file pointers. There is a read file pointer and a write file pointer, but for ordinary files the filebuf
class uses the same pointer for both actions, so changing one of them also changes the other one.
pubseekpos()
The pubseekpos(position, pointer)
method moves the file pointer to a specified position relative to the start of the file and returns the new position. The pointer property specifies whether to move the read pointer, the write pointer or both by using the following flags:
ios_base::in
- Move the read pointer.ios_base::out
- Move the write pointer.
The |
operator can be used to combine both flags like this: ios_base::in|ios_base::out
cout << MyFileBuf.pubseekpos(4, ios_base::in);
pubseekoff()
The pubseekoff(offset, origin, pointer)
moves the file pointer to a specified position given by an offset relative to a specified origin and returns the new position.
The origin parameter must be one of the following values:
ios_base::beg
- Offset relative to the beginning of the file.ios_base::cur
- Offset relative to the currend file pointer position.ios_base::end
- Offset relative to the end of the file.
The pointer property specifies whether to move the read pointer, the write pointer or both by using the following flags:
ios_base::in
- Move the read pointer.ios_base::out
- Move the write pointer.
The |
operator can be used to combine both flags like this: ios_base::in|ios_base::out
cout << MyFileBuf.pubseekoff(-5, ios_base::end, ios_base::in);
File Reading Functions
in_avail()
The in_avail()
method returns the number of characters available to be read in the file.
cout << MyFileBuf.in_avail();
snextc()
The snextc()
method moves the file pointer foward by one character and returns the ASCII value of the character at the new position.
cout << MyFileBuf.snextc();
sbumpc()
The sbumpc()
method returns the ASCII value of the character at the current position and moves the file pointer foward by one character.
cout << MyFileBuf.sbumpc();
sgetc()
The sgetc()
method returns the ASCII value of the character at the current position without moving the file pointer.
cout << MyFileBuf.sgetc();
sgetn()
The sgetn(destination, n)
method reads n characters from the file and writes them into the char
array specified by the destination parameter. This method returns the number of characters which were read.
char destination[20];
int amount = MyFileBuf.sgetn(destination, 19);
destination[amount] = '\0'; // Add a null terminating character to the string
cout << destination;
File Writing Functions
sputc()
The sputc()
method writes a character at the current position and then moves the file pointer forward by one character. This method returns the ASCII value of the character that was written.
cout << MyFileBuf.sputc();
sputn()
The sputn(source, n)
method writes n characters from the char
array specified by the source parameter into the file. The file pointer is moved forward by n characters. This method returns the number of characters that were written to the file.
char source[] = "Hello World!";
int num = MyFileBuf.sputn(source, 12);
cout << num << " characters were written to the file";