AravindG
  • AravindG
EXAM PREPARATION DOUBTS : endl-Inserts a new-line character. Additionally, for buffered streams, endl flushes the buffer (i.e. writes all unwritten characters in the buffer to the output sequence,) .. why do we need to flush the buffer can i get an example so that I understand the use of endl ?Is endl a function .If yes what is its header file ? Its my final lab exam today ! Pray for me ! :)
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
AravindG
  • AravindG
@Opcode
AravindG
  • AravindG
how is endl useful when working with files ?
Opcode
  • Opcode
How do I explain this... Okay. So endl means end line and it renders the output so the next output will start from a new line. ``` int main() { cout << "I sleep." << endl; cout << "On a bed."; return 0; } Output: I sleep. On a bed. Now if you removed endl and your code looked like: int main() { cout << "I sleep. "; cout << "On a bed."; return 0; } Output: I sleep. On bed. ``` So you can think of endl like the enter key to make a new line.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

AravindG
  • AravindG
well then whats the difference between "\n" and endl ?
Opcode
  • Opcode
Silly :3 endl is only for C++. \n is for C you use the \n in printf to do the same thing. (Make the output look the same.)
AravindG
  • AravindG
but what do they say abt the buffer part ?
AravindG
  • AravindG
why is cleaning buffer necessary ?
Opcode
  • Opcode
Flushing the buffer is necessary, because the user will see the output written to the stream immediately. Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character. Flushing means emptying the buffer and actually writing it to the device. Check this: http://stackoverflow.com/questions/4751972/c-endl-and-flushing-the-buffer Read: (Benjamin Lindley's answer)
AravindG
  • AravindG
so when all should i use endl when working with files ?
AravindG
  • AravindG
i usually us \n for my programs
Opcode
  • Opcode
\n doesn't flush output buffer... endl does.
Opcode
  • Opcode
So if your using files I'd use endl. But otherwise I'd go with \n
AravindG
  • AravindG
flushing of buffer increases speed?
Opcode
  • Opcode
Somewhat. And especially when working with files. "That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character."
AravindG
  • AravindG
Question 2: My following code always gives o/p "as file does not exist" any solution ?
AravindG
  • AravindG
#include #include #include void main() { char f1[20],f2[20]; char ch; cout<<"\n\nEnter original file name:"; cin>>f1; cout<<"\nFile copy:"; cin>>f2; ifstream in(f1); ofstream out(f2); if(in.fail()) { cout<<"File does not exist "; exit(1); } while(!in.eof()) { in>>ch; out<
rsmith6559
  • rsmith6559
std::endl is more portable than hard coding '\n'. Even after all these years Unix uses a linefeed, Windows uses carriage return line feed together and some Mac GUIs still use carriage returns to end a line. std::endl is probably defined as whatever the OS specifies for ending a line.
AravindG
  • AravindG
@Opcode ....
AravindG
  • AravindG
urgent :can u identify error in my program below its not working either #include #include class distance { int m,cm; public: void read(); void operator +(distance); void display(); }; void distance::read() { cout<<"\n\nEnter distances:"; cin>>m>>cm; } void distance::display() { cout<<"\n distances:"; cout<=100) { temp.m+=1; temp.cm=(temp.cm%100); } } void main() { distance d3,d4,d5; d3.read(); d4.read(); d5=d3+d4; d5.display(); getch(); }
AravindG
  • AravindG
@Opcode
AravindG
  • AravindG
well my teacher told me instead of using for loop to input data to file char c[200] outfile out; for(i=0;c[i]!='\0';i++) { cin>>c[i]; out<
Opcode
  • Opcode
Wait. So like: ``` #include using namespace std; int main() { string input; while (true) { cout << "Type your input: "; while (getline(cin, input)) cout << input << endl; } return 0; } ```
AravindG
  • AravindG
i dunno is that the method ?

Looking for something else?

Not the answer you are looking for? Search for more explanations.