UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Problem parsing file

#1 2008-01-09 05:27 PM

db_appz
Member
Registered: 2007-10-06
Posts: 6

Re: Problem parsing file

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#define MYFILE "mtty.txt"
#define PP 80

char *get_pattern(char *buf, char *pattern, char *dlm);
char *convert_datet(char *datee, char *timee);
char buf[PP], *bn, *tok, *bsn, w[18], **tbn;
char *delim = " \t";
FILE *fp;
int i =0, j = 0, k = 0;
static int cnt = 0;
typedef struct {
       char bsname[7];
       char cname[7];
       char date_down[16];
       char downtime[14];
       char kpi[5];
       char status[20];
       } outage;
        
//outage **bt;
outage bt[100];

char *get_pattern(char *buf, char *pattern, char *dlm)
{
 char *inp, *delm, *lfor;
 inp = buf;
 delm = dlm;
 lfor = pattern;
 tok = strtok(inp,delm);
 while(tok != NULL)
 {
  if(strstr(tok,lfor) != NULL)
  {
   return tok;
   break;
   }/*if(strstr(tok,lfor) != NULL)*/
   tok = strtok(NULL,delm);
  }/*while(tok != NULL)*/ 
 }/*char *get_bsname(char *buf, char *pattern, char *dlm)*/

char *convert_datet(char *datee, char *timee)
{
 char *dt, *tim, tt[5];
 dt = datee;
 tim = timee;
 w[11] = '\0';
 w[10] = ' ';
 w[9] = '2';
 w[8] = '0';
 w[7] = *dt;
 w[6] = *(dt+1);
 w[5] = '-';
 w[4] = *(dt+2);
 w[3] = *(dt+3);
 w[2] = '-';
 w[1] = *(dt+4);
 w[0] = *(dt+5);
 tt[0] = *tim;
 tt[1] = *(tim+1);
 tt[2] = *(tim+2);
 tt[3] = *(tim+4);
 tt[4] = '\0';
 strcat(w,tt);
 return w;
}/*char *convert_datet(char *datee, char *timee)*/

int main()
{
 if ((fp = fopen(MYFILE,"r")) == NULL)
 {
  printf("Can't open MYFILE\n");
  exit(-1);
  }/* if ((fp = fopen(MYFILE,"r")) == NULL) */
  
  //bt = (outage **)malloc((i+1)*sizeof(*bt));
  //if(bt == NULL) exit(-1);

while(fgets(buf,sizeof(buf),fp) != NULL)
{
 buf[strlen(buf)+1] = '\0';
 if (strcmp(buf,"\0") == 0) continue;
 if (strstr(buf,"<<<<<") != NULL)
 { 
  bsn = get_pattern(buf,"BS",delim);
  strcpy(bt[i].bsname,bsn);
  //printf("%s\n",bt[i].bsname);
  
  while((strstr(buf,"<QUIT") == NULL) && (!feof(fp)))
  {
   if (fgets(buf,sizeof(buf),fp) != NULL)
   {
    buf[strlen(buf)+1] = '\0';
    if (strcmp(buf,"\0") == 0) continue;
     
    if (strstr(buf,"A1") != NULL)
    {
     int n = 0;
     char *ddate, *ttime, *tk;
     tk = strtok(buf,delim);
     while(tk != NULL)
     {
      if((n>2) || (n<5))
      {
       if (n = 3) ddate = tk;
       if (n = 4) ttime = tk;
       tk = strtok(NULL,delim);
       n++;
       }/*if((n>2) || (n<5))*/
       tk = strtok(NULL,delim);
      }/*while(tok != NULL)*/
      //printf("%s\n",ddate);
      //printf("%s\n",ttime);
     }/*if (strstr(buf,"A1") != NULL)*/
     
    if (strstr(buf,"BCCH") != NULL)
    {
     char *cl, *bh, *tk;
     tk = strtok(buf,delim);
     while(tk != NULL)
     {
      cl = tk;
      tk = strtok(NULL,delim);
      break;
      }/*while(tk != NULL)*/
      strcpy(bt[i].cname,cl);
      printf("%s\n",bt[i].cname);
     }/*if (strstr(buf,"BCCH") != NULL)*/
    }/*if (fgets(buf,sizeof(buf),fp) != NULL)*/
    i++;
    strcpy(bt[i].bsname,bsn);
    printf("%s\n",bt[i].bsname);
   }/*while(strstr(buf,"<QUIT") == NULL)*/
  }/* if (strstr(buf,"<<<<<") != NULL) */
  cnt = i++;
 }/* while(fgets(buf,sizeof(buf),fp) != NULL) */
fclose(fp);
 
for (i=0;i<cnt;i++)
{
 printf("%s\t%s\t%s\t%s\t%s\t%s\n",bt[i].bsname,bt[i].cname,bt[i].date_down,bt[i].downtime,bt[i].kpi,bt[i].status);
}
 
 return 0;
}/* int main () */

Offline

#2 2008-01-09 06:58 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Problem parsing file

Have you tried running your program under gdb or valgrind?  Either of those will often tell you what line of code is segfaulting, and occasionally even why.

Offline

  • Index
  • » C
  • » Problem parsing file

Board footer

Powered by FluxBB