8 Replies - 6458 Views - Last Post: 09 April 2010 - 09:34 AM Rate Topic: -----

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

C++ unbuffered File IO

Posted 09 February 2010 - 03:07 PM

I have a C program in a C book that uses low level C functions to do unbuffered file IO, and am wondering if fstream can do unbuffered file IO as well??

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <conio.h>

int main()
{
	int file_descriptor,readsize;
	char buffer[256];
	memset(buffer,0,256);
	file_descriptor=open("test.txt",O_RDONLY | O_TEXT);
	readsize=read(file_descriptor,buffer,256);
	buffer[255]=0;
	printf("%s",buffer);
	close(file_descriptor);
	_getch();	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ unbuffered File IO

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ unbuffered File IO

Posted 09 February 2010 - 03:48 PM

Yes, see istream::read
Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ unbuffered File IO

Posted 09 February 2010 - 05:17 PM

Sorry, I forgot to mention, I'm trying to do unbuffered writing... Also, where in that page does it say anything about unbuffered?

istream::readsome Read block of data available in the buffer (public member function)

This post has been edited by taylorc8: 09 February 2010 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ unbuffered File IO

Posted 09 February 2010 - 06:08 PM

Quoting the cite I gave you:

Quote

Read block of data
Reads a block of data of n characters and stores it in the array pointed by s.

If the End-of-File is reached before n characters have been read, the array will contain all the elements read until it, and the failbit and eofbit will be set (which can be checked with members fail and eof respectively).

==========================================================
Notice that this is an unformatted input function and what is extracted is not stored as a c-string format, therefore no ending null-character is appended at the end of the character sequence.
==========================================================

Calling member gcount after this function the total number of characters read can be obtained.

Read() behaves just like your C example read() except that it doesn't return a count of bytes read.

This post has been edited by n8wxs: 09 February 2010 - 06:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ unbuffered File IO

Posted 09 February 2010 - 07:21 PM

Can you guarantee me an unbuffered write operation using the member function write() ? The whole focus of my question is the *unbuffered* part. Could you supply a page supporting your post?

This post has been edited by taylorc8: 09 February 2010 - 07:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: C++ unbuffered File IO

Posted 09 February 2010 - 07:47 PM

Define what you mean by "unbuffered". All <iostream> IO utilizes stream buffer(s). The read() and write() functions do not use formatted IO the way that the "<<" and ">>" operators do. They allow the use to do block IO without regard to record boundaries. If you write a record of 100 bytes, then 200 and another 100 bytes, your reading program has to know what the record sizes are to correctly recover the data written when read.

This post has been edited by n8wxs: 09 February 2010 - 07:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ unbuffered File IO

Posted 09 February 2010 - 08:59 PM

When writing to a file, if the data to be written is too small it is stored in a buffer and written when there is more data. But thanks for your help, I think I'll be fine with the classes provided. I will do a simple test to see if the data is written immediately with ofstream.write(); (use write for one character, check if it has been written then close the stream)
Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ unbuffered File IO

Posted 09 February 2010 - 09:07 PM

To summarize the book on C i'm reading without plagiarizing it, the book says the open function is used for unbuffered IO, and said something about copying 1k of data into a 64k buffer makes no sense. (back in the day)

I did the test, and the data wasn't written until the file stream was closed, meaning it was sitting in a buffer!!!
I don't think it's unbuffered IO with the ofstream.write();

however, io.h write() for the C language is.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_coder*


Reputation:

Re: C++ unbuffered File IO

Posted 09 April 2010 - 09:34 AM

Usually file streams will buffer data, even when using their write() and read() calls.
To disable it, try setting the buffer backed by the stream to zero size, for example by using
ofstream of; of.rdbuf()->pubsetbuf(0,0);
This should disable buffering if the filebuf implementation allows it.
Was This Post Helpful? 0

Page 1 of 1