473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

fstream buffering


I'm perusing Langer/Kreft trying to find a solution to an issue here.
Thus far, it's some heavy reading. In any event, given:

int main()
{
std::ofstream ofs ( "my_file.txt" );
if (!ofs )
return EXIT_FAILURE;

for ( int idx ( 0 ); idx < 10 ; ++idx )
{
ofs << idx
}

ofs.close ();
}

I'd like to turn off 'buffering' . How would i achieve that?

Aug 10 '06 #1
1 3834
ma740988 wrote:
>
I'm perusing Langer/Kreft trying to find a solution to an issue here.
Thus far, it's some heavy reading. In any event, given:

int main()
{
std::ofstream ofs ( "my_file.txt" );
if (!ofs )
return EXIT_FAILURE;

for ( int idx ( 0 ); idx < 10 ; ++idx )
{
ofs << idx
}

ofs.close ();
}

I'd like to turn off 'buffering' . How would i achieve that?
try:

#include <fstream>

int main()
{
std::ofstream ofs ( "my_file.txt" );
if (!ofs )
return EXIT_FAILURE;

ofs.rdbuf()->pubsetbuf(0,0);

for ( int idx ( 0 ); idx < 10 ; ++idx )
{
ofs << idx;
}

ofs.close ();

}

From the standard:

basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n);
Returns: setbuf(s,n)

For file based stream buffers [27.8.1.4/10]

basic_streambuf* setbuf(char_type* s, streamsize n);
Effects: If setbuf(0,0) is called on a stream before any I/O has occured
on that stream, the stream becomes unbuffered. Otherwise the results are
implementation-defined. "Unbuffered" means that pbase() and pptr() always
return null and output to the file should appear as soon as possible.

Best

Kai-Uwe Bux
Aug 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
7
by: Mathias Herrmann | last post by:
Hi. I have the following problem: Using popen() to execute a program and read its stdout works usually fine. Now I try to do this with a program called xsupplicant (maybe one knows), but I dont...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
2
by: Acer | last post by:
platform: freeBSD(gcc) Win32(MinGW) I try follows: char cbuf; ifstream fin; fin.rdbuf()->pubsetbuf(cbuf,buf_size); but I found it do not influence the I/O times when I increased or...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
6
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.