I have a text file that looks something like this
user1|password1
user2|password2
I have a string, say "user1|password1". I need to compare each line of the text file with this string.
FILE *fp;
fp = fopen("user_info_file.txt", "r");
while(fgets(str, 10, fp)!=NULL){
n = write(newsockfd, "STR:", 4);
n = write(newsockfd, str, 15);}
And then go on to strcmp. I use the write function because I'm having a server write this to a client.
I've just put 15 as a random value (it is within bounds) to see if any text is being read. But when it prints str, well str doesn't contain anything. It just prints as "STR:" and then I get my prompt back on the next line.
Please tell me how to read the data in user_info_file.txt (at least the first line) into str. I've spent hours on this but I just can't seem to get it. Newbie here, please bear with me.
EDIT: Basically I'm creating an IM application. User registers by entering their username and password, which is sent to the server and the server stores it in user_info_file in the form I've written above. After a user registers, they need to be able to login. They enter username and password, which again I send to the server. I then need the server to compare this string with the existing entries in user_info_file to validate the login. So I need the server to check every line of user_info_file and compare it to what the user enters.
So I have
char* str = new char[256];
FILE *fp;
fp = fopen("user_info_file.txt", "r");
while(fgets(str, 10, fp)!=NULL){
n = write(newsockfd, "STR:", 4);
n = write(newsockfd, str, 15);}
if(strcmp(buffer, str) == 0)
n = write(newsockfd, "Logged in", 9);
else
n = write(newsockfd, "No such username", 16);
Basically I am trying to read each line of user_info_file into str. Then I compare str after every read line with buffer. buffer stores the login info the client has sent. If they are a match, then the login should be successful.
But, when I write str, well it doesn't print anything. 15 is the number of characters from str it should write, correct? So even if the number was, say, 5, it should print at least "user1" or "user2" or the first 5 characters of SOME line of user_info_file. But it doesn't look like anything is getting read from the file into str at all.
I hope I've made it clear. What can I do? I'm at my wit's end.