im trying to create a simple LAN Chat Program (as projectwork for school)....usage of filehandling is a must in all projects...since all the school computers are connected by LAN...i am creating one FILE and trying to input and read that file and display it on the screen to make it LIKE a chat room.(see the program below).

#include<all.h>
void main()
{
clrscr();
ofstream fout;
char uname[21],t[101],u[101];
fout.open("Chat.txt");
fout<<"Connecting...\nConnection Established\nWELCOME TO THE C++ CHAT ROOM\nRules:\nType \'exit\' to go out of the chat room.\n\n";
fout.close();
cout<<"Enter username : ";
gets(uname);
strcpy(u,uname);
ifstream fin;
start:
clrscr();
fin.open("Chat.txt");
while(fin.eof()==0)
{
fin.getline(t,101);
cout<<t<<endl;
}
fin.close();
fout.open("Chat.txt",ios::app);
cout<<uname<<" : ";
gets(t);
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();
}

Two users can simultaneously run the program on two diff computers .so what one types gets stored in the file - "chat.txt"(shared documets-accessible by both the comps)...THE PROBLEM IS...user1 types a msg it stores in chat.txt ....which is not immediately displayed in user2's window but only after user2 enters the message he is to be sending (gets(t)) .after entering it goes to label 'start' and freshly opens the file again for reading....that is...user2 is able to view the msg that user1 typed only after he types the message.

is there any way how the msg a user types gets recieved immediately even though the reciever does not enter a msg.

NOTE: THIS CAN BE TESETED IN A SINGLE COMPUTER BY OPENING TWO WINDOWS OF THE exe FILE CREATED BY C++ and exchanging msgs.( im using Turbo C++).

waiting for help and comments

hmmm, ok, sorry, but your ideea with the file being the chatroom or whatever just isn't good !!
First of all, you have to synchronize all acces to that file. When one user is writing the file all others must wait. When one user wants to print the "chatroom" he has to transfer the whole file throug the LAN( why do this? why copy an entire file on the network when you just need the last message? ) This must also be synchronized... U just do enormous transfers with no reason. The whole design is not good.

What you have to do is do some reasearch on soket programming (linux or windows) and TCP/IP. If you do have to use a file, use it on the server as a list of subscribed users with passwords and stuff, some kind of account manager. If you find socket programming difficult you could try RPC ( the implementation would be easyer ) or even use boost ( as far as I know boost's asio, a portable wraper over sockets, similar to java's Socket class )

http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046476070&id=1043284351
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044841143&id=1043284376

Then decide whether you're compiling C or C++, rather than just whatever happens to compile as you think of it.

Also replace the 'goto' with a suitable while loop.

Oh, and also learn about code indentation.
http://en.wikipedia.org/wiki/Indent_style

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.