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

Attachments
/* fpont 1/00 */
/* pont.net    */
/* tcpServer.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> /* close */
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


#define SUCCESS 0
#define ERROR   1

#define END_LINE 0x0D  
#define SERVER_PORT 23
#define MAX_MSG 100

/* function readline */
int read_line();


/*MAIN STARTS*/

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

{
  

  char rdr;
  char *saveptr; 
  FILE *fd;
  int sd, newSd, cliLen,j,lop1,lop2,tst;

  struct sockaddr_in cliAddr, servAddr;
//  char *str1, *str2, *token, *subtoken;
//  char *saveptr1, *saveptr2;

  char line[MAX_MSG], fstrg[MAX_MSG], token[MAX_MSG];


  /* create socket */
  sd = socket(AF_INET, SOCK_STREAM, 0);
 	  if(sd<0) {
 	   perror("cannot open socket ");
 	   return ERROR;
 		 }
  
  /* bind server port */
  servAddr.sin_family = AF_INET;
  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(SERVER_PORT);
  
	  if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
	    perror("cannot bind port ");
	    return ERROR;
		  }

  listen(sd,5);


  
  while(1) {

    printf("%s: waiting for data on port TCP %u\n",argv[0],SERVER_PORT);

    cliLen = sizeof(cliAddr);
    newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
    if(newSd<0) {
      perror("cannot accept connection ");
      return ERROR;
	    }
    
    /* init line */
    memset(line,0x0,MAX_MSG);
    


//////////////////////////////////////// Reading

if (read_line(newSd,line)!=ERROR)
{
      	printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);
}
else
printf ("Error reading \n");


///Opening File

		fd = fopen(line, O_RDONLY);

		if(fd==-1)
		   {
		     printf("\n\n\t\t Error CREating a File");
		     perror("errno");
		   }



memset(line,0x0,MAX_MSG);

  /* receive segments */
    while(read_line(newSd,line)!=ERROR)   

//	if(read_line(newSd,line)!=ERROR)
{    
      	printf("%s: received from %s:TCP%d : %s\n", argv[0], inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), line);

////// search string 



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

fgets(fstrg, 100, fd);




token = strtok_r(fstrg, argv[1], &saveptr);

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


}



/*tst = FindStr(fd, line);
if (tst != -1)
printf("Command Found\n");
else
printf("Command Not Found");

/**********************************************************/
      /* init line */
      memset(line,0x0,MAX_MSG);
      
    } /* while(read_line) */
    
  } /* while (1) */



		 close(fd);
}


/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */
/* this function is experimental.. I don't know yet if it works  */
/* correctly or not. Use Steven's readline() function to have    */
/* something robust.                                             */
/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING       */

/* rcv_line is my function readline(). Data is read from the socket when */
/* needed, but not byte after bytes. All the received data is read.      */
/* This means only one call to recv(), instead of one call for           */
/* each received byte.                                                   */
/* You can set END_CHAR to whatever means endofline for you. (0x0A is \n)*/
/* read_lin returns the number of bytes returned in line_to_return       */
int read_line(int newSd, char *line_to_return) {
  
  static int rcv_ptr=0;
  static char rcv_msg[MAX_MSG];
  static int n;
  int offset;

  offset=0;

  while(1) {
    if(rcv_ptr==0) {
      /* read data from socket */
      memset(rcv_msg,0x0,MAX_MSG); /* init buffer */
      n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */
      if (n<0) {
	perror(" cannot receive data ");
	return ERROR;
      } else if (n==0) {
	printf(" connection closed by client\n");
	close(newSd);
	return ERROR;
      }
    }
  
    /* if new data read on socket */
    /* OR */
    /* if another line is still in buffer */

    /* copy line into 'line_to_return' */
    while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) {
      memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1);
      offset++;
      rcv_ptr++;
    }
    
    /* end of line + end of buffer => return line */
    if(rcv_ptr==n-1) { 
      /* set last byte to END_LINE */
      *(line_to_return+offset)=END_LINE;
      rcv_ptr=0;
      return ++offset;
    } 
    
    /* end of line but still some data in buffer => return line */
    if(rcv_ptr <n-1) {
      /* set last byte to END_LINE */
      *(line_to_return+offset)=END_LINE;
      rcv_ptr++;
      return ++offset;
    }

    /* end of buffer but line is not ended => */
    /*  wait for more data to arrive on socket */
    if(rcv_ptr == n) {
      rcv_ptr = 0;
    } 
    
  } /* while */
}

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 :)

This question has already been answered. Start a new discussion instead.