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:
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
#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);
}