LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2006, 06:41 AM   #1
Deepak Inbasekaran
Member
 
Registered: Apr 2006
Location: India
Distribution: Red Hat Linux release 9 (Shrike)
Posts: 44

Rep: Reputation: 15
index searching


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.
 
Old 08-11-2006, 04:51 PM   #2
uselpa
Senior Member
 
Registered: Oct 2004
Location: Luxemburg
Distribution: Slackware, OS X
Posts: 1,507

Rep: Reputation: 47
OMG what a mess. Are you sure that C is the right language for you?

Just for comparison, doesn't this look better?
Code:
#!/usr/bin/env python
import sys
index='Index<%s> .\n'%sys.argv[2]
for line in file(sys.argv[1],'rU'):
    if line.endswith(index):
        print line
Used as follows:
Code:
pu@slackw:~/projects/temp1$ ./getmsg.py log1.txt 3
Apr 13 05:29:46 SYSTEM sample message3||Index<3> .

pu@slackw:~/projects/temp1$ ./getmsg.py log2.txt 5
Apr 13 05:29:46 SYSTEM sample message5||Index<5> .

pu@slackw:~/projects/temp1$ ./getmsg.py log2.txt 8
Apr 13 05:29:46 SYSTEM sample message8||Index<8> .
I think you would greatly benefit from learning a simpler (and yet more powerful) language such as Python which I used as an example here.

Just a thought.
 
Old 08-16-2006, 11:49 PM   #3
Deepak Inbasekaran
Member
 
Registered: Apr 2006
Location: India
Distribution: Red Hat Linux release 9 (Shrike)
Posts: 44

Original Poster
Rep: Reputation: 15
oh ya but my project is in C , so got to go with it
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing icon theme? i have a index.theme and index.desktop, no directions! mr_coffee Linux - Newbie 2 01-25-2006 01:45 PM
dynamic index? @ngelot Programming 2 09-24-2005 12:23 PM
KMail index Calgarian SUSE / openSUSE 1 03-12-2005 06:02 AM
index.php help greenmeanie Debian 5 03-09-2005 02:31 AM
missing index hardgriffith Mandriva 1 09-17-2003 07:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration