HeavySoul 27 Newbie Poster

It's little things like this that make me really hate coding sometimes!!
That works great!!
If I was going to nit pick though I would say that when I print out the string from file it still comes out like:
"1|bob|123
"
but at this point I really dont care bout that! The comparision works and with multiple entries.

Thanks to everyone that replied!!

HeavySoul 27 Newbie Poster

Ok, something new, this code works but only if the file has 1 entry. If there is more than 1, same problem again, even if I try to compare the first entry in file!

int i = 0, j = 0, k = 0, pos = 0;

	printf("Opening database...\n");

	fp = fopen("clients1.txt", "a+");
	fseek(fp, 0, SEEK_END);
	pos = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	char charin[pos];

	if(fp == NULL)
	{
		printf("Error:	Failed to open database\n");
		return 0;
	}

	while(fgets(charin, pos, fp) != NULL)
	{
		printf("Checking...\n");
		printf("\"%s\"\n", charin);
		printf("\"%s\"\n", buffer);
		if(charin[strlen(charin)-1] == '\0')
			charin[strlen(charin)-1] = 0;
		j = strcmp(charin, buffer);
		if(j == 0)
		{
			printf("Client is already registered\n");
			k = 1;
		}
	}

	printf("Closing database...\n");
	fclose(fp);

Please, please help me finish this of!

HeavySoul 27 Newbie Poster

Sorry Walt didn't see your comment at first. The BUFFER needs to be large enough to hold a client and IP address, both of which are of an unknown length, so the BUFFER will need to be large enough to hold them. Saying that the string held in the BUFFER, received from the client program prints fine!!!
It is with the string read in from the file where I am having problems! Even though the string from file prints as:
"1|bob|123
"
and the string from the client prints as:
"1|bob|123"
strcmp returns the value that the string from the file is smaller, than the string from the client. Why is this? How can I solve it? Is there a better method to use to achieve this?

HeavySoul 27 Newbie Poster

Isn't the fgets() function supposed to go through the whole file though? Or at least (in this case) until it comes to a NULL? It doesn't seem to be going past that first line and is still being read as:
"
"
What could be causing this?

HeavySoul 27 Newbie Poster

Ok, my code so far:

while(fgets(charin, BUFFER, fp) != NULL)
{
printf("Checking...\n");
printf("\"%s\"\n", charin);
printf("\"%s\"\n", buffer);
if(charin[strlen(charin)-1] == '\0')
charin[strlen(charin) -1] = 0;
if(strcmp(charin, buffer) == 0)
{
printf("Already exists\n");
}
else
{
printf("Writing\n");
fprintf(fp, buffer);
}
}

If the file being read is empty the following will be printed to screen:

"
"
so writes the string from the client to the file. If the program is run again with data in the file, I still get:

"
"

which the first line in the file is empty space, as the data written to file before is on the next line down.

HeavySoul 27 Newbie Poster

Following your advise Dragon, I enclosed the text on both the string being read in from file and string received from the client. When printed to screen the string from the file looks like:
"temp
"
and the string from the client:
"1|bob|123"

I agree that these strings would not match any and that the client should be written to file, however the "temp" is that the only data in the file, yet it seems to be the only thing read in!

What else am I missing?

HeavySoul 27 Newbie Poster

Thanks for the replies so far!
To answer the questions, I do not believe it is case sensitive issue, as everything is in lower case. BUFFER was defined as 1024 in size.
As a side note, when I used the function strlen to compare the size of each string, for some reason the string read in from the file was smaller than the string received from the client. However when both are printed out to the screen, they are identical.

E.g. of string from file

1|bob|123

Can anyone suggest why two identical strings would be different sizes?

HeavySoul 27 Newbie Poster

Hi people, my first post here, hope its a good one.
I am trying to develop a program which will simulate a very simple name server, using the client-server frame.
My problem appears after the server receives the data (in form of a string) from the client and tries to compare this to strings within a file. The server should see if the data already exists, leaving file unchanged or enter into the file if it doesnt. I have used the functions strcmp(which always says that the data isnt in the file, even when it is) and strstr(which says that the data is in the file, even if the file is blank!).

Could someone please tell me what am I doing wrong?!
Thanks

while(fgets(charin, BUFFER, fp) != NULL)
{
if(strcmp(charin, buffer) == 0)
printf("already exists\n");
else
fprintf(fp, buffer);
}
Ancient Dragon commented: Good work using code tags the first time :) +27