Dear all,

I am trying to build a code which finds a string from an opened file ,, but in order to find it it must tokenize every line first.. here is the code segment, If any of u can help plz..

#define MAX_MSG = 100

int main (int argc, char *argv[]) 

{
  
  char rdr;
  int j, fd, lop1,lop2,tst;
  char line[MAX_MSG], fstrg[MAX_MSG], token[MAX_MSG];

/* some code**/


fd = open(line, O_RDONLY);

/**some code*/

memset(fstrg, '\0', MAX_MSG);
lop1 = 0;
while (read(fd, &rdr, 1) == 1)
{
fstrg[lop1] = rdr;
if (rdr == '\n' || rdr == EOF)
break;
}




token = strtok(fstrg, '#');

if ( strcmp(token, line) == 0)
{
	while (read(fd, &rdr, 1) == 1)
	{
		write(newSd, &rdr,1);
		if (rdr == '#')
			break;
	}
break;


}

> #define MAX_MSG = 100
A segmentation fault means you ran it, and it crashed.
Whereas the code you posted won't even compile (not with that #define).

Also, use fgets() to read a whole line for you (and fopen to open the file).

thanks for the advice, I have tried but still no use,, the above snippet was just for reference as it was the part where segmentation fault occured ( as per my view) attached with is the complete code and it is giving an error in using strtok as well. Seeking reply

I don't have any intention of debugging your whole code, but...

lop1 = 0;
while (read(fd, &rdr, 1) == 1)
{
fstrg[lop1] = rdr;
if (rdr == '\n' || rdr == EOF)
break;
}

fstrg[lop1] is always fstrg[0]

Thanx Aia but I asked about Segmentation Fault which occurs precisely at the mentioned place indicated below, the remaining errors occur only because I am also making amendments to correct it so at times I skip things.

while(1)
{

fgets(fstrg, 100, fptr);
token = strtok(fstrg, &delim);

if (strcmp(token, line) == 0)
{
printf("String FOUND");
break;
}

else
printf("String NOT FOUND:");

}

(1) strtok() assumes both its arguments are strings that are terminated with a 0 character. Behaviour is undefined if that is not true.

2) strtok also returns NULL if it cannot find a token being searched for. strcmp() yields undefined behaviour if either argument is NULL.

3) A segmentation fault is one possible symptom of undefined behaviour.

4) The fact that you are observing a segmentation fault is occurring at a "precise point" is irrelevant. The cause of segmentation faults is not necessarily the line where they are reported: the actual cause can be any code executed at or before that point.

Thanks Grumpier!!

Thanks to Salem and Aia ,, with the combine help of u all I think I have solved this problem :)