Detail of read() and write()

I want to know about the internal working of read() and write() functions in ifstream and ofstream respectively. I,although, have made many programs of these function but i want to get the details of internal processes.
What write(char*,ofstream::pos_type); of the ofstream class does is to put or write into a new file the content of the first argument. And the second one is to tell the function the number of characters(from 0--the beginning) to write into the new file.

read(char*,ofstream::pos_type); also does the same things(with the same parameters), but it belongs to ifstream and therefore copies the content of the opened file, to the first character. The second argument also serves the same purpose.

Hope it Helps!
@Aceix, thanx for the explanation. But I knew this.All i want to know is about what happens in the compiler when we deploy these functions. and about the interaction of the physical file with compiler...
The important things to remember are:

a. read() and write() functions should only be used for i/o of trivially copyable objects - ie. objects which can be safely copied with std::memcpy()

1
2
3
4
5
6
7
8
9
template < typename T >
typename std::enable_if< std::is_trivially_copyable<T>::value, void >::type
write_to_stream( const T& object, std::ostream& stm )
{ stm.write( reinterpret_cast<const char*>( &object ), sizeof(object) ) ; }

template < typename T >
typename std::enable_if< std::is_trivially_copyable<T>::value, void >::type
restore_from_stream( T& object, std::istream& stm )
{ stm.read( reinterpret_cast<char*>( &object ), sizeof(object) ) ; }


b. The streams should opened should be in binary mode, or else escape sequence translations would take place.

c. Use of read() and write() compromises transparency; it makes testing and debugging code a lot harder.
Topic archived. No new replies allowed.