A
amit
Hi guys!I am trying to write a program which will segregate some
selected keywords from a given file.The source code is given alongwith
#include<stdio.h>
#include<string.h>
char key_set[][4]={"ami\0","inc\0","lud\0"};
#define MAX_KW_SIZE 10
#define MAX_OCUR 100
struct kbuf_node{
char keyword[MAX_KW_SIZE];
int byte_offset[MAX_OCUR];
}buf_node;
int main(int argc,char *argv[])
{
FILE *fp,*fp_kbuf;
char *s;
int i,offset=0,cnt,FLAG=0;
fp_kbuf=fopen("buf.txt","wb"); //the file in which i will save the
keywords and their byte offsets
for(i=0;i<=2;i++)
{
cnt=0;
offset=0;
s=(char*)malloc(sizeof(key_set));
fp=fopen(argv[0],"rb"); //the master file given as argument
if(fp==NULL)
{
printf("Unable to open file");
exit(0);
}
while(fgets(s,sizeof(key_set),fp)!=NULL)
{
//printf("Before setpos%u\n",ftell(fp));
offset++;
if(strcmp(s,key_set)==0)
{
//printf("%s\t %d\n",s,offset);
if(FLAG==0)
{
strcpy(buf_node.keyword,s);
FLAG=1;
}
buf_node.byte_offset[cnt++]=offset;
}
fsetpos(fp,&offset);
//printf("After setpos %u\n",fp);
}
//write to the kw-buffer file
fwrite(&buf_node,sizeof(buf_node),1,fp_kbuf);
FLAG=0;
fclose(fp);
}
fclose(fp_kbuf);
fp_kbuf=fopen("buf.txt","rb");
while(fread(&buf_node,sizeof(buf_node),1,fp_kbuf)>0)
{
printf("\n%s\t",buf_node.keyword);
for(i=0;i<cnt;i++)
printf("%d\t",buf_node.byte_offset);
printf("\n");
}
return 0;
}
After compiling the file when i run the program using a C source file
as my file then it works fine.BUt with a text file it only reports the
first occurence of the keywords!I am using gcc under FC5
selected keywords from a given file.The source code is given alongwith
#include<stdio.h>
#include<string.h>
char key_set[][4]={"ami\0","inc\0","lud\0"};
#define MAX_KW_SIZE 10
#define MAX_OCUR 100
struct kbuf_node{
char keyword[MAX_KW_SIZE];
int byte_offset[MAX_OCUR];
}buf_node;
int main(int argc,char *argv[])
{
FILE *fp,*fp_kbuf;
char *s;
int i,offset=0,cnt,FLAG=0;
fp_kbuf=fopen("buf.txt","wb"); //the file in which i will save the
keywords and their byte offsets
for(i=0;i<=2;i++)
{
cnt=0;
offset=0;
s=(char*)malloc(sizeof(key_set));
fp=fopen(argv[0],"rb"); //the master file given as argument
if(fp==NULL)
{
printf("Unable to open file");
exit(0);
}
while(fgets(s,sizeof(key_set),fp)!=NULL)
{
//printf("Before setpos%u\n",ftell(fp));
offset++;
if(strcmp(s,key_set)==0)
{
//printf("%s\t %d\n",s,offset);
if(FLAG==0)
{
strcpy(buf_node.keyword,s);
FLAG=1;
}
buf_node.byte_offset[cnt++]=offset;
}
fsetpos(fp,&offset);
//printf("After setpos %u\n",fp);
}
//write to the kw-buffer file
fwrite(&buf_node,sizeof(buf_node),1,fp_kbuf);
FLAG=0;
fclose(fp);
}
fclose(fp_kbuf);
fp_kbuf=fopen("buf.txt","rb");
while(fread(&buf_node,sizeof(buf_node),1,fp_kbuf)>0)
{
printf("\n%s\t",buf_node.keyword);
for(i=0;i<cnt;i++)
printf("%d\t",buf_node.byte_offset);
printf("\n");
}
return 0;
}
After compiling the file when i run the program using a C source file
as my file then it works fine.BUt with a text file it only reports the
first occurence of the keywords!I am using gcc under FC5