Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Anyone familiar with read/write to a file in C?
For my networks class we are programming a chat application in C. I have everything pretty much down, I think. Except searching a text file.

It is a client server chat application. Basically multiple clients chatting using one server as the middle man. Anyway the server is storing a text file of the client info such as their IP, username, etc.

I already created the file and used a pointer to write to it using fprintf. However how do I search the file for a specific username?

Like if a new chat client tries to log in with a username already taken I am supposed to search through the text file for the username, and if it is already taken prompt the client to enter another name.

So basically how do you search a text file for a specific string in C? Like search for the specific username?

So I know how to read and write to a text file. For reading I know you can use fscanf. But what about searching?

Oh and using multiple threads is not as hard as I thought it would be. Just have to use mutex to lock and unlock threads to keep from having 2 clients send something at the same time to the server thus overwriting a struct that you don't want to overwrite yet.

Sponsored links

Use a while loop where the condition is !feof(FILE). !feof() is basically not end of file and FILE is the name of the variable you used to read/write the file.
Nappa: Vegeta! What does the scouter say about his power level?
Vegeta: It' thousand and six.
Nappa: Wh-...really?
Vegeta: Yeah! Beat him up Nappa!
Nappa: Yay!
So far I am just writing to the file but also storing the client info in an array of structs so that way it is easier to search and keep track of client IP, who is chatting with who, etc.

Real pain but I am almost done.

Users browsing this thread: 1 Guest(s)