I need a complete help on how to use the flush() member function with ofstream, ifstream and fstream and also its use.
P.S: Please provide an example for me to understand
flush() is for use with output streams only. It is not to be used on input streams. I/O is buffered in C++ and flush() forces an output to an appropriate variable clearing of the output stream buffer. There is no similar built in function for input streams, though you can write something that accomplishes basically the same thing if you want. The syntax I've seen used most commonly is in conjunction with a new line char in a manipulator called endl.
int i = 10;
cout << i << endl;
which would be equivalent to
int i = 10
cout << i << '\n' << flush;
flush() is for use with output streams only. It is not to be used on input streams. I/O is buffered in C++ and flush() forces an output to an appropriate variable clearing of the output stream buffer. There is no similar built in function for input streams, though you can write something that accomplishes basically the same thing if you want. The syntax I've seen used most commonly is in conjunction with a new line char in a manipulator called endl.
int i = 10;
cout << i << endl;which would be equivalent to
int i = 10
cout << i << '\n' << flush;
Now suppose I have the following code which is LAN program
void main()
{
clrscr();
ofstream fout;
char uname[21],t[101],u[101];
fout.open("//Redneck/c/lanchat/chat.lan");
fout<<"Connecting...\nConnection Established\nWELCOME TO THE C++ CHAT ROOM";
fout<<"\nRules:\nType \'exit\' to go out of the chat room.\nHit the \'enter\' key to refresh"; fout<<"messages.\n\n";
fout.close();
cout<<"Enter username : ";
gets(uname);
strcpy(u,uname);
ifstream fin;
start:
clrscr();
fin.open("//Redneck/c/lanchat/chat.lan");
while(fin.eof()==0)
{
fin.getline(t,101);
cout<<t<<endl;
}
fin.close();
fout.open("//Redneck/c/lanchat/chat.lan",ios::app);
cout<<uname<<" : ";
gets(t);
if(strcmp(t,"")==0)
{
fout.close();
goto start;
}
if(strcmpi(t,"exit")==0)
{
cout<<"\nPress any key...";
getch();
exit(0);
}
strcat(u," : ");
strcat(u,t);
fout<<u<<endl;
strcpy(u,uname);
fout.close();
delay(500);
goto start;
getch();
}
I use a blank string i.e input is only enter to refresh the screen and then display the message typed by an other user. Can I use flush() to get the output immediately without refreshing the screen i.e as soon as input is received?. If yes, please use it in the above program.
I'm sorry, I don't understand your question since I don't know what you mean by an LAN program (and I've never written for multiuser programs) or what you mean by refresh the screen. You are using the non-standard clrscr() to clear the screen of any previously printed material, so I guess you mean putting new material to the screen when you say refresh, but I'm not sure about that.
You are already using endl, so you are already using flush in your program.
Some observations not related to your posted questions:
1) When posting code to this board use code tags to retain the indentation I hope you use when writing your code. Some very respected responders/members won't review/respond to your post if it's not formatted since it's just too hard to read.
2) it's int main(), not void main()
3) I'm a bit suspicious of this:
while(fin.eof()==0)
I'd try to take the eof() call out of there and try not to assume that 0 == false, though it usually does
4) I'm not sure where you go back to with the goto calls (goto calls are usually best avoided if at all possible).
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.