Questions tagged [filebuf]

`std::filebuf` template specification in C++

A filebuf object opens and manages a file pointer and serves as the stream buffer for fstream objects.

std::filebuf is actually a typedef for std::basic_filebuf<char, std::char_traits<char> >.

Wide-character file streams use std::wfilebuf which is a typedef for std::basic_filebuf<wchar_t, std::char_traits<wchar_t> >.

Use this tag for questions about the stream buffer used by std::fstream, std::ifstream, std::ofstream and their wide-character equivalents. For questions about those classes themselves use , or . For more general questions use the or tags.

22 questions
5
votes
0 answers

basic_filebuf::sync(): Is this a mistake on cppreference.com?

The page for std::basic_filebuf::sync() says that sync() or its equivalent is implicitly called by ... seekoff(), and seekpos() ... But I cannot seem to find any mention of this in the language spec, neither here or here. My read of the filebuf…
Geezer
  • 5,600
  • 18
  • 31
4
votes
2 answers

Explain Change to GNU C++ filebuf::underflow() interacting with filebuf::seekoff()

My company's products run on a number of qualified Linux hardware/software configurations. Historically, the compiler used has been GNU C++. For purposes of this post, let's consider version 3.2.3 the baseline, as our software 'worked as expected'…
Don Wakefield
  • 8,693
  • 3
  • 36
  • 54
4
votes
1 answer

Not able to ofstream using __gnu_cxx::stdio_filebuf

This creates the file but it doesn't write anything. std::ofstream outstream; FILE * outfile; outfile = fopen("/usr7/cs/test_file.txt", "w"); __gnu_cxx::stdio_filebuf filebuf(outfile,…
3
votes
3 answers

std::fstream with multiple buffers?

You can specify one buffer for your file stream like that: char buf[BUFFER_SIZE]; std::ofstream file("file", std::ios_base::binary | std::ios_base::out); if (file.is_open()) { file.rdbuf()->pubsetbuf(buf, BUFFER_SIZE); file <<…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
2
votes
0 answers

std::filebuf open file, create if it doesn't exist

I want to open a file for read and write with std::filebuf, but create the file if it doesn't exist yet. If it already exists, do nothing. I want to freely write into the file, without any "always jump to end before each write" behavior. What open…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
2
votes
1 answer

Using same stream object to write to filestream or stringstream

I am trying to use an ostream object to write to either to a filestream of stringstream based user input (similar to fmemopen in Linux). I realized that ostream doesnt take stringstream or fstream objects but instead takes stringbug or filebuf. I…
Trancey
  • 699
  • 1
  • 8
  • 18
2
votes
1 answer

Detect loss of integrity of the underlying file when using std::filebuf?

I have some code which uses std::ifstream to read from a file, but doesn't use any of the formatting features provided by std::ifstream. It essentially does something like this: std::ifstream in; in.open("example.txt", std::ios_base::in |…
Bernard
  • 5,209
  • 1
  • 34
  • 64
2
votes
0 answers

How to properly check the result of copying file while using std::fstream and std::filebuf

I would like to use widely known code snippet to copy file: std::ifstream src("in"); std::ofstream dest("out"); dest << src.rdbuf(); The question is: how to properly check if copying succeded? I thought that checking the value of (src && dest) is…
Mati
  • 61
  • 6
2
votes
1 answer

Customized "ofstream" output

I have to extend the ofstream class to write a logging stream service. The goal is to intercept each line, adding at the head of each one a customized text (date/time, severity, and so on). Now, it's clear that I'm not a C++ guru, so I read a lot of…
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
1
vote
1 answer

std::filebuf passed to std::ifstream not always called

The Goal Have unbuffered I/O and disabled kernel page cache for a legacy C++11 program. This feature must be on demand (through an executable parameter). The idea is to reduce the memory overhead of I/O operations regardless of performances. I am…
A. Gille
  • 912
  • 6
  • 23
1
vote
2 answers

How to write data(binary mode) in to a file only using std::filebuf?

Please examine following code : #include #include #include int main() { char mybuffer[512]; std::filebuf* optr = new std::filebuf(); optr->pubsetbuf(mybuffer, 512); const char sentence[] = "Sample…
Buddhika Chaturanga
  • 957
  • 2
  • 14
  • 29
0
votes
1 answer

Pass a pointer to a file buffer to a class, expecting to read from file inside a class

I want to learn how to search in the file by passing the pointer of the stream to a class. I can successfully get the first character from the file using std::fstream and std::filebuf* char symbol; std::fstream…
0
votes
1 answer

When is calling basic_filebuf::pbackfail allowed/defined to succeed

On implementing a basic_filebuf I stumbled over basic_filebuf::pbackfail and don't fully understand its definition. From cplusplus.com Moves the current input position on position back to point to the previous character and, if supported, makes c…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
0
votes
0 answers

Deadlock on std::filebuf open method between two threads

I have a c++ application which has multiple daemon threads running infinitely which does some file logging and rotation. This application was working fine when it was compiled with VS2010. But when i upgraded and compiled it with VS 2017, there is a…
0
votes
0 answers

Auto cout each cin

I am trying to print automatically each variable which gets input from cin. class MyFileBuf : public std::filebuf { protected: virtual std::streamsize xsgetn (char* p, streamsize n) { std::streamsize ret = std::filebuf::xsgetn( p, n…
GalRoimi
  • 37
  • 5
1
2