LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to find a particular string occurance position in a file? (https://www.linuxquestions.org/questions/programming-9/how-to-find-a-particular-string-occurance-position-in-a-file-716312/)

vigneshinbox 04-02-2009 07:43 AM

How to find a particular string occurance position in a file?
 
I have a program which will write informations like gid,gname,resname,restype,etc... in a file.I wrote a function to get the starting and ending position of a particular group.Stating of a particular group (say group 12)is denoted in a file by #gid12 and the end of the group is denoted by @gid12.I don't know wat mistake i have done. The code works correctly only for the 1st group in the file. when i try to get other groups information i am getting wrong output.Plz tel me wat is the bug in my program.And is there any other way to find the starting and ending position of a particular group from my file.

OUTPUT FILE WILL LOOK AS FOLLOWS:


Quote:

IP_Address:12.12.12.12
#gid1:sdf
Res-info$:;xyz;qww;qasddfasd;2:
Res-info$:;hjuy;asdfv;dfhvhvf;3:
Res-info$:;asdfg;eretg;wedsfh;7:
@gid1
IP_Address:123.12.132.12
#gid2:dfc
Res-info$:;hjuy;asdfv;dfhvhvf;3:
Res-info$:;asdfg;eretg;wedsfh;7:
@gid2
IP_Address:121.121.112.12
#gid12:xsdf
Res-info$:;xyz;qww;qasddfasd;2:
Res-info$:;asdfg;eretg;wedsfh;7:
@gid12


I have given the code below.Is there any other way to get the starting and ending position of a particular group from my file?? kindly help me on this

Code:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
#define LINESIZE 256

struct resource
{
char res_type[12];
char res_name[25];
char res_loc[30];
int res_pri;
}res[MAX];
int store(char *ip_add,int g_id,char *g_name,struct resource res[],int n);

int FindStr(FILE *f, char *str);

main()
{
int g_id,i=0,choice,nor,j,status,gi=0,st=0,en=0;
char *g_name,*ip_add;
FILE *fp;



g_name=(char *)malloc(sizeof(char) * (MAX+1));
ip_add=(char *)malloc(sizeof(char) * (16));
printf("\nEnter the Following Data...\n");
printf("\nEnter Node IP Address:");
scanf("%s",ip_add);
printf("Enter g_id:");
scanf("%d",&g_id);
printf("Enter g_name:");
scanf("%s",g_name);
printf("Enter the resources...\n");

do
{
printf("Enter %d res_type:",i+1);
scanf("%s",res[i].res_type);

printf("Enter %d res_name:",i+1);
scanf("%s",res[i].res_name);

printf("Enter %d res_location:",i+1);
scanf("%s",res[i].res_loc);

printf("Enter %d res_priority:",i+1);
scanf("%d",&res[i].res_pri);


printf("\nDo u want to add another resource?\n 1.Yes\n2.N0\n:");
scanf("%d",&choice);

i++;
}while(choice==1);
nor=i-1;
j=0;


status=store(ip_add,g_id,g_name,res,nor);
if(status==0)
printf("\nfile updated successfully...\n");
else
printf("\nUpdate failed...\n");

 const char sbase[] = "#gid";
 const char ebase[] = "@gid";
 char start [10],end[10];
 //read the group id to be fetched
 printf("Enter the Group id to be fetched:");
 scanf("%d",&gi) ;
 sprintf(start, "%s%d", sbase, gi);
 printf("start = \"%s\"\n", start);
 sprintf(end, "%s%d", ebase, gi);
 printf("end = \"%s\"\n", end);
 fp=fopen("config.ini","r");

 st=FindStr(fp, start);
 en=FindStr(fp, end);
 printf("\nStart position:%d",st);
 printf("\nEnd position:%d",en);




}


struct group
{
char ip_add[25];
int g_id;
char g_name[25];
struct resource res[];
}g;

int store(char *ip_add,int g_id,char *g_name,struct resource res[],int n)
{
FILE *fp;
int i=0,j=0;
//ip_add=(char *)malloc(sizeof(char) * (MAX+1));
fp=fopen("config.ini","a");
if (fp==NULL)
{
printf("cannot open file");
return 1;
}
else
{
strcpy(g.ip_add,ip_add);
g.g_id=g_id;
strcpy(g.g_name,g_name);

while(i<=n)
{
//g.res[i].res_type=res[i].res_type;
strcpy(g.res[i].res_type,res[i].res_type);
strcpy(g.res[i].res_name,res[i].res_name);
strcpy(g.res[i].res_loc,res[i].res_loc);
g.res[i].res_pri=res[i].res_pri;
i++;
}



fprintf(fp,"\nIP_Address:%s",g.ip_add);
fprintf(fp,"\n#gid%d:",g.g_id);
fprintf(fp,"%s",g.g_name);

while(j<=n)
{
fprintf(fp,"\nRes-info$:;");
fprintf(fp,"%s;",g.res[j].res_type);
fprintf(fp,"%s;",g.res[j].res_name);
fprintf(fp,"%s;",g.res[j].res_loc);
fprintf(fp,"%d:",g.res[j].res_pri);
j++;
}
fprintf(fp,"\n@gid%d",g.g_id);
fclose(fp);
return 0;
}


}


int FindStr(FILE *f, char *str)
{
 int s_pos; //string position in the text
 int c_pos; //char position in the text
 char *string;
 char ccnt; //char count
 
 s_pos = -1;
 c_pos = 0;
 string = malloc(strlen(str));
 while (!feof(f))
  {
  if (c_pos == 0)
    for (ccnt = 1; ccnt <= strlen(str); ccnt++)
    if (!feof(f))
      string[ccnt - 1] = getc(f);

  if (c_pos != 0)
    if (!feof(f))
    {
      for (ccnt = 0; ccnt <= strlen(str) - 2; ccnt++)
      string[ccnt] = string[ccnt + 1];
      string[strlen(str) - 1] = getc(f);
    } 
  if (strcmp(string, str) == 0)
    {
    s_pos = c_pos;
    break;
    } 
  c_pos++;
  }
 return(s_pos);
}


Sergei Steshenko 04-02-2009 08:31 AM

Why "C" and not, say, Perl with the power of built-in regular expressions ?

Telemachos 04-02-2009 09:49 AM

Please don't double post.

Also, as Sergei said, unless this is homework for a class on C, I cannot image why you would want to do this in C. A language like Perl, Python or Ruby would be much easier.

jiml8 04-02-2009 03:10 PM

It is actually very simple in C, but not when you use the scanf function.

Just read the whole file into a buffer, then use the strstr function and pointers. You can pull everything out that you want, and quickly.

Is this homework, by the way?

vigneshinbox 04-02-2009 11:33 PM

Quote:

Originally Posted by jiml8 (Post 3496459)
It is actually very simple in C, but not when you use the scanf function.

Just read the whole file into a buffer, then use the strstr function and pointers. You can pull everything out that you want, and quickly.

Is this homework, by the way?

Ya it is.If possible can u give a brief explaination plz??

jiml8 04-02-2009 11:52 PM

strstr will return the first instance of the substring in a string. So you read the whole file into a buffer, then search for the first instance of your substring using strstr. After you find the first instance, you set a pointer to point to the end of that substring, then repeat strstr using that pointer as the beginning of the string you are searching. Strstr will then return the next instance of the substring.

Just repeat this in a loop until you have found all instances. The pointer values returned by strstr are the distance into the file you have gone to find the information you are looking for.


All times are GMT -5. The time now is 05:17 AM.