Re: [Dev-C++] Newbie Q " /n " or " endl" ?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Michael D. S. <for...@hi...> - 2003-08-04 21:51:24
|
> "the results are stored in memory (this is for reasons of speed). endl
makes sure that they (the
> files or devices) are updated."
Well sorta'. It does flush it out of the buffer but then the channel
program picks it up and it also has a queue. A networked printer at a big
company could develop a queue during the day long enough to print well into
the night but printers are very slow. With a disk it's generally seconds or
at most minutes but that's still forever to the CPU, the channel program
queue (disk swap area) keeps it from having to wait. OK, as much, even
writing to main memory is slow to the cpu.
Mostly this detail doesn't matter but if you're programming CGI and
wondering why your files are getting corrupted, this is the reason. A
second hit tries to read before the write from the first has cleared the
queue. Because of the queue and because a shared server has a lot of other
traffic (also in the queue), this doesn't take near as much traffic to your
site as you might think. A hundred hits a day can do it, mostly because
much like the freeways everyone seems to want to go at once.
File locks are the answer to that problem -- if you can get 'em to work.
Usually 8k is the most efficient buffer size for a disk drive and if you're
writing that much or more you should allow it use it's optimum buffer size.
If you're only writing a few lines or you have a need for speed, for CGI or
whatever reason, a buffer flush is what you want.
Still I wouldn't do this:
cout << "line one"
<< endl
<< "line two"
<< endl;
but rather this:
cout << "line one\nline two"
<< endl;
I know that's so little and the computer is so fast that it's practically
irrelevant, but I like to feel I'm doing the best I can anyway:)
ms
If at first you don't succeed, destroy all evidence that you tried
At 03:51 AM 8/4/03, you wrote:
>To just add a small amount to Abhijit's reply, think of it like this: '\n'
>(note: _not_ '/n') is just a character, but endl is a function that can do
>all sorts of things in addition to just adding a character or two to the
>output stream.
>
>Abhijit Shylanath wrote:
>
>>Hello, Graham.
>>
>>
>>
>>>My book defines "/n" as new line.
>>>And defines " endl " as end line.
>>>But gives me no clue as to what the difference is or when to use which.
>>>can anyone explain the difference to me ?
>>>
>>
>>Two differences you should know:
>>
>> '\n' is new-line to C, and to Unix, but may not be new-line to your OS.
>>For example, for Windows, "\r\n" is new-line. IIRC, for macs, it's just
>>'\r'. endl takes care of these platform specific thingies for you (as a
>>matter of fact, so does '\n' in files opened in text mode in STDIO, but endl
>>is a more graceful and conformant solution.
>>
>> Next, endl makes sure that the text that you've written is flushed. Most
>>streams have a buffer, so that when you write to them, the connected device
>>or file is not updated instantly, but the results are stored in memory (this
>>is for reasons of speed). endl makes sure that they (the files or devices)
>>are updated.
>>
>> If in doubt, use endl. If you're working with raw data (binary files or
>>such), then you will have to use '\n'. When you start working with binary
>>files, you should be able to tell the difference anyway, if your book is any
>>good.
>>
>>Hope this helps.
>>
>>--
>>Abhijit Shylanath
>>Homepage: http://mudeth.tripod.com/
>>
>>
>>-------------------------------------------------------
>>This SF.Net email sponsored by: Free pre-built ASP.NET sites including
>>Data Reports, E-commerce, Portals, and Forums are available now.
>>Download today and enter to win an XBOX or Visual Studio .NET.
>>http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
>>_______________________________________________
|