LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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:33 AM   #1
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Rep: Reputation: 0
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);
}
 
Old 04-02-2009, 07:56 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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;
}

Last edited by dwhitney67; 04-02-2009 at 07:59 AM.
 
Old 04-02-2009, 08:02 AM   #3
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Original Poster
Rep: Reputation: 0
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

Last edited by vigneshinbox; 04-02-2009 at 08:08 AM.
 
Old 04-02-2009, 08:14 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by vigneshinbox View Post
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.
 
Old 04-02-2009, 08:19 AM   #5
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
Old 04-02-2009, 09:06 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by vigneshinbox View Post
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.

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.

Last edited by dwhitney67; 04-02-2009 at 09:07 AM.
 
Old 04-02-2009, 11:28 PM   #7
vigneshinbox
LQ Newbie
 
Registered: Mar 2009
Posts: 19

Original Poster
Rep: Reputation: 0
Thumbs up

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

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...
 
  


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
nvidia problem when ending current session farmerdave Slackware 4 09-15-2008 03:27 AM
file ending with .tlt tostay2003 Programming 5 08-23-2007 02:53 AM
My never ending FC3 sound card problem!! Fredstar Linux - Newbie 5 05-03-2005 08:47 PM
Removing Text in a single line starting with one pattern ending on another mgwheeler Programming 13 08-03-2004 04:36 PM
how to uncompress a file ending with diff.gz tamari Linux - Software 3 06-16-2002 12:46 PM

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

All times are GMT -5. The time now is 04:47 AM.

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