Click to See Complete Forum and Search --> : size of line grabbed from fgets() call


seeweed
November 17th, 2001, 12:42 PM
Hello again,

For those, who only wants to know my question,
read this, for those who wants to get more back
ground on it keep reading:
--[ QUESTION
is there a way of knowing the size of a line, in
a file, for example:
If file "victor.txt" has a line like this:
"victor12345"
I want to be able to know, that the specific line
has 11 bytes in it. Anyone?



Im coding an application that at some point will
need to open a file with reading attributes, and
read line by line and compare two string using a string search
algorithm, if the string inside the line grabbed
from fgets, exist inside a string that comes from
a user input.

So, i've placed a call like this on my code,
(consider that the file is already opened and can
be viewed with fp descriptor)

char line[300];
while(fgets(line, sizeof(line), fp) != NULL) {
searchalgorithm(line,outsidebuffer);
}

Now here come's my problem, when i do a call like
the fgets one i did above, it will put the content
of the line inside "line", however if line is < 300 bytes, then it will (by security measures, i think) fill up the restant of bytes with null chars, so looking from a program point of view,
if the string inside the file fp is: "seeweed",
with that fgets function what char line will hold is: "seeweed+293 NULL CHAR's", however if a user
types "seeweed" only, i will try to find:
"seeweed+293NULL chars" inside "seeweed", what
obviuslly doesnt exist...


So after this long story ;), here is my question,
is there a way of knowing the size of a line, in
a file, for example:
If file "victor.txt" has a line like this:
"victor12345"
I want to be able to know, that the specific line
has 11 bytes in it.

Anyone can help?
[]'z setuid

Chris Benson
November 18th, 2001, 02:32 AM
Hi,

First, fgets returns to you a NULL terminated string - period. Whatever follows the NULL (regardless of if you satisfied your 300 byte read or not) is undefined and depends on the CRT implementation. Don't make any assumptions about what comes after the NULL terminator.

The way you describe your search algorithm seems to be the problem. The way you describe it sounds like it's doing a block memcmp rather than a substring search (which is what I think your intention is).

If you want to find out if a substring exists in any given string, you can take any number of approaches, but basically you want something like this:


char *pszSubstring = strstr(pszLineFromFile, pszUserInput);

if(pszSubstring)
{
// found match
}




Hope this helps...
-Chris

seeweed
November 18th, 2001, 08:08 AM
I'm pretty sure that the problem isn't with the
search algorithm, and i'm not using strstr(),
im using the Boyer Moore String Seach Algorithm,
are you familiar with it? Anyhow, it's clear for
me that the problem is with the fgets() function,
since if the first line of my file is "victor",
which is 6 bytes long, and i do this:

if(fgets(line, 6, fp) != NULL) {
searchalgorithm(line,userinput);
}

then is will find "victor" inside the userinput
perfectelly.

You could help me, by telling me how can i know
the size of a line, so that i can tell fgets to
only that number of bytes into char line.

Any ideias?

James Curran
November 18th, 2001, 08:33 AM
The only way to know the length of a string returned by fgets() is to do a strlen() on the buffer after the return. Note also, that fgets() includes the \n at the end of the line, so if the file was:victor12345
helloworld

Then the first fgets() will return 12 characters.



Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

seeweed
November 18th, 2001, 11:40 AM
Hmmmm... but think about this,
char bleh[300];
fgets(bleh, sizeof(bleh), fp);
Will copy 299 bytes from a line of fp to bleh, right? I belive that this is the case, just like
strncpy calls where, if the buffer isn't totally
filled than we fill the restant with null chars
until it reach it's end.

How ever what i want to be able to do is this:
1o. Discover how many bytes there are in one line,
2o. Do a fgets() call like this:
fgets(buffer, NUMOFBYTESONLINE,fp);

Got it? Any help, please?

James Curran
November 18th, 2001, 11:57 AM
In reply to:

Will copy 299 bytes from a line of fp to bleh, right?



Yes, assuming there are at least 299 character in the line.

In reply to:

I belive that this is the case, just like strncpy calls where, if the buffer isn't totally filled than we fill the restant with null chars until it reach it's end.



Yes for strncpy, no for fgets. fgets merely reads up to the end of the line or till the number of characters is reached. It does NOT fill past the end of the line.
In reply to:

1o. Discover how many bytes there are in one line



The only way to do that would be to read one character at a time, using fgetc, looking for a \n, the then rewind, and use fgets. Not really practical, but I'll still not sure why this is even needed.



Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.