nextline statement in write function

C

ChrisC

if I had outfile.write((char*) record, sizeof( record ))
how would I get write to return a newline?
as it is it doesn't.
 
V

Victor Bazarov

ChrisC said:
if I had outfile.write((char*) record, sizeof( record ))
how would I get write to return a newline?
as it is it doesn't.

You're doing it again. 'write' member is not supposed to
return a newline. Its return value type is 'ostream&', so
it can only return the stream it's called for.

If you are talking of the _side_effects_ of calling 'write',
then, no, there will be no "newline" unless your 'record'
[accidentally] contains it.

'ostream::write' simply puts the characters (the whole
second argument of them), located at the first argument
address, into the stream for which 'write' is called. Yes,
byte by byte, directly. That's called "unformatted output".
Any "newline" would mean that you need _formatted_ output.
For that you should use operator<< and provide one for your
type, overloaded.

V
 
T

Thomas Matthews

ChrisC said:
if I had outfile.write((char*) record, sizeof( record ))
how would I get write to return a newline?
as it is it doesn't.

Try this program:
#include <iostream>
#include <cstdlib> // using EXIT_SUCCESS
#include <fstream>

using namespace std;

const char example_text[] = "first line.\nsecond line.\n";

int main(void)
{
cout.write(example_text, sizeof(example_text) - 1);
return EXIT_SUCCESS;
}

TH009MA@th009ma-shl2-01 /cygdrive/d/Temp
$ bcc32 -o junk junk.cpp
Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
junk.cpp:
Turbo Incremental Link 5.60 Copyright (c) 1997-2002 Borland

TH009MA@th009ma-shl2-01 /cygdrive/d/Temp
$ ./junk.exe
first line.
second line.

As you can see, the write() method is printing a newline,
BECAUSE THE NEWLINE IS IN THE TEXT STRING.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top