ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i have a code to retrieve strings based on their index
i have 2 files with indexed msgs
Quote:
log1.txt
Apr 13 05:29:46 SYSTEM sample message1||Index<1> .
Apr 13 05:29:46 SYSTEM sample message2||Index<2> .
Apr 13 05:29:46 SYSTEM sample message3||Index<3> .
Apr 13 05:29:46 SYSTEM sample message4||Index<4> .
and another file
Quote:
log2.txt
Apr 13 05:29:46 SYSTEM sample message5||Index<5> .
Apr 13 05:29:46 SYSTEM sample message6||Index<6> .
Apr 13 05:29:46 SYSTEM sample message7||Index<7> .
Apr 13 05:29:46 SYSTEM sample message8||Index<8> .
Now i got to write a code that gets an input from user and then it retrieves the corresponsing message
i tried my best still there are some errors, can anyone debug it ?
code :
Quote:
#include<stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
DIR *default_dir = NULL ;
struct dirent *ent_default = NULL;
FILE *fd;
int first_index = 0;
int last_index = 0;
int log_index = 0;
int index = 0;
char *temp=(char *)malloc(100);
char *ptr=(char *)malloc(100);
char *msg = (char *)malloc(100);
char *log_msg = (char *)malloc(100);
char *ptr_index_end=(char *)malloc(100);
char *ptr_index_start=(char *)malloc(100);
char *out, last[20];
default_dir = opendir("/home/inbasek/test");
if(NULL == default_dir)
{
printf(" opendir failed ");
exit(0);
}
out = (char *)malloc(100);
memset(temp,'\0',100);
memset(log_msg,'\0',100);
memset(last,'\0',sizeof(last));
memset(msg,'\0',100);
memset(out,'\0',100);
memset(ptr_index_end,'\0',100);
memset(ptr_index_start,'\0',100);
memset(ptr,'\0',100);
while((ent_default = readdir(default_dir)) != NULL)
{
if((strstr(ent_default->d_name,"log")) != NULL)
{
/* open file with matching file name */
if(( fd = fopen(ent_default->d_name,"r")) == NULL)
{
printf(" fopen error");
continue;
}
printf("enter index to be searched : \t");
scanf("%d",&index);
/* get first msg */
if ((fgets(msg,100,fd)) == NULL)
{
printf(" fgets failed for %s ", ent_default->d_name);
continue;
}
/* get first index */
if((msg != NULL) && ((ptr = strstr(msg,"Index<"))!= NULL))
{
/* move pointer to end of Index< */
ptr += 6;
/* store current pointer value in temporary pointer */
*ptr_index_start = *ptr; /* ptr1 changed to ptr_index_start & pt -> ptr_index_end */
/* search for > */
while(*ptr != '>')
{
*ptr_index_end = *ptr;
ptr++;
ptr_index_end++;
}
/* get the index value */
while(*ptr_index_end != *ptr_index_start)
ptr_index_end--;
/* index convert integer */
first_index = atoi(ptr_index_end);
printf(" first Index is %d\n",first_index);
}
/* get last index value */
fseek(fd , 0 , SEEK_END);
/* get back to Index< start */
fseek(fd ,-15, SEEK_CUR);
/* read from Index< till end */
fgets(last,15,fd)!=NULL;
out = strstr(last,"Index<");
temp = (char *)strtok(out,"<");
temp = (char *)strtok(NULL,">");
/* store last index value as integer */
last_index = atoi(temp);
printf("last Index is %d\n",last_index);
}
printf( "compare with first and last log msg ");
if ( (index >= first_index)&& ( index <= last_index))
{
while(1)
{
/* get each log msg */
if ((fgets(log_msg,100,fd)) == NULL)
{
printf(" fgets failed ");
continue;
}
/* get index */
if((log_msg != NULL) && ((ptr = strstr(log_msg,"Index<"))!= NULL))
{
/* move to end of Index< */
ptr += 6;
/* copy current pointer value to another temp pointer */
*ptr_index_start = *ptr;
/* search for > and increment pointer */
while(*ptr != '>')
{
*ptr_index_end = *ptr;
ptr++;
ptr_index_end++;
}
/* read back in reverse to get the index value */
while(*ptr_index_end != *ptr_index_start)
ptr_index_end--;
/* index convert to integer */
log_index = atoi(ptr_index_end);
printf(" Index is %d\n",log_index);
if ( log_index == index )
{
printf(" Matching log msg : %s",log_msg);
exit(1);
}
}
else /* check for next file */
{
break;
}
}
}
else
{
continue;
}
}
closedir(default_dir);
}
Last edited by Deepak Inbasekaran; 08-11-2006 at 06:42 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.