[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Oprettelse af en .rc fil



In article <sslug@sslug> you wrote:

....
>   while ( fgets(lnbuf,BUFFERLEN,stdin) ) {
>      *dummy='\0';
>      if ((n=sscanf(lnbuf,"%30[a-zA-Z]=%80[^\n]",var,val))<2) 
> 	n=sscanf(lnbuf,"%30[a-zA-Z]%*[^=]=%80[^\n]",var,val);
>      printf("*%d [%s] = [%s]\n",n,var,val);
>   }

Kan gøres bedre, se nedenfor.

Men skal man anvende lidt mere avanceret input
så er "flex" (lex) løsningen. 
Er det endnu mere avanceret så er det bison + flex (yacc+lex)


#include <stdio.h>
#include <ctype.h>

#define BUFFERLEN 512
#define VARLEN 30
#define VALLEN 80

void notrspaces(char *src) {
/* remove leading/trailing spaces and other garbage  */
 char *s;
 if (!src || ! *src ) return;
 for(s=src+strlen(src)-1; s>src && isspace(*s); --s) ;
 *(s+1)='\0';
 for(s=src; *s  && isspace(*s); ++s) ;
 if (s==src) return;
 for( ; *s; *src++ = *s++);
 *src='\0';
}

int main()
{
  char lnbuf[BUFFERLEN];
  char var[VARLEN+1];
  char val[VALLEN+1];
  int n;
  while ( fgets(lnbuf,BUFFERLEN,stdin) ) {
     *var=*val='\0';
     /* n==2 if input ok */
     n=sscanf(lnbuf,"%30[^=\n\r]=%80[^\n\r]",var,val);
     notrspaces(var);
     notrspaces(val);
     printf("%d: [%s] = [%s]\n",n,var,val);
  }
  return 0;
}



-- 
mvh Frank Damgaard  | http://home3.inet.tele.dk/frda/



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *