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 04-02-2009, 07:43 AM   #1
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Rep: Reputation: 0
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);
}
 
Old 04-02-2009, 08:31 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Why "C" and not, say, Perl with the power of built-in regular expressions ?
 
Old 04-02-2009, 09:49 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.
 
Old 04-02-2009, 03:10 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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?
 
Old 04-02-2009, 11:33 PM   #5
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jiml8 View Post
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??
 
Old 04-02-2009, 11:52 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
  


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
Replacing character position within the string of a file scroy Linux - Newbie 13 11-08-2008 05:45 PM
How to find a match string in conf file Ameii83 Linux - Newbie 6 08-12-2008 07:42 PM
How to find occurance of word in one line atul_sp Linux - Server 5 07-21-2008 01:20 PM
how to find out particular string in a file mksc Linux - Newbie 1 06-23-2008 03:04 AM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM

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

All times are GMT -5. The time now is 08:41 PM.

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