LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem in getting the starting and ending of a particular record from a file. (https://www.linuxquestions.org/questions/programming-9/problem-in-getting-the-starting-and-ending-of-a-particular-record-from-a-file-716309/)

vigneshinbox 04-02-2009 07:33 AM

Problem in getting the starting and ending of a particular record from 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);
}


dwhitney67 04-02-2009 07:56 AM

Can you please explain this statement:
Quote:

OUTPUT FILE WILL LOOK AS FOLLOWS:
Does your output file look correct, or is it a goal that you are trying to achieve? You don't seem sure because you used the word "will".

You can verify the contents of your output file using the command 'cat filename'.

If your file is correct, and you want to find a string within the file and reports its position, then a combination of ftell() and fgets(), and of course feof(), should to the trick.

Code:

int FindStr(FILE* fp, const char* str)
{
  // assume fp is a valid file pointer
  // assume str is the string being sought

  int pos = -1;

  while (!feof(fp))
  {
    char line[256] = {0};  // 256 was arbitrarily chosen

    int curLoc = ftell(fp);

    fgets(line, sizeof(line), fp);

    if (strncmp(line, str, strlen(str) == 0)
    {
      pos = curLoc;
      break;
    }
  }

  return pos;
}


vigneshinbox 04-02-2009 08:02 AM

ya the output file will look like wat i have posted before.I need to do some manipulations with that output file only.

specifically i need to find the position of #gid12 and @gid12

dwhitney67 04-02-2009 08:14 AM

Quote:

Originally Posted by vigneshinbox (Post 3496008)
ya the output file will look like wat i have posted before.I need to do some manipulations with that output file only.

specifically i need to find the position of #gid12 and @gid12

Try using the FindStr() function I provided earlier.

vigneshinbox 04-02-2009 08:19 AM

Thanx for ur reply dude.But i tried the function which u have posted.But it returns -1 for all the strings.i.e, its not finding the specified string in the file.

dwhitney67 04-02-2009 09:06 AM

Quote:

Originally Posted by vigneshinbox (Post 3496022)
Thanx for ur reply dude.But i tried the function which u have posted.But it returns -1 for all the strings.i.e, its not finding the specified string in the file.

The FindStr() does not work because it has a bug in it. :p

Sorry about that... replace the strncmp() line with:
Code:

    if (strncmp(line, str, strlen(str)) == 0)
The original statement was missing a closing parenthesis for the strncmp() function call.

vigneshinbox 04-02-2009 11:28 PM

Quote:

Originally Posted by dwhitney67 (Post 3496066)
The FindStr() does not work because it has a bug in it. :p

Sorry about that... replace the strncmp() line with:
Code:

    if (strncmp(line, str, strlen(str)) == 0)
The original statement was missing a closing parenthesis for the strncmp() function call.


Thanx dude... it worked...


All times are GMT -5. The time now is 03:18 AM.