[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] gets strengen



Sune Fischer wrote:
> 
> Hej
> 
> Jeg får en række kommandoer ind via stdin fra Xboard (et skak GUI)
> 
> char *linein;
> gets(linein);

gets() er farlig - læs man-siden! Brug fgets i stedet.
> 
> jeg skal have analyseret "linein" for de kommandoer den indeholder.
> 
> Umiddelbart kan jeg ikke se andet, end at det er noget værre biks,
> jeg havde forestille mig at gøre noget i retning af:
> 
> memcpy(target,linein,8);
> if(strcmp(target,kommando1)==0) // så er det kommando 1
> 
> osv. med alle tænkelige kommandoer, indtil den er fundet.
> 
> Nu er problemet, at kommandoen kan jo sagtens kan være på færre end 8 tegn,
> hvordan tjekker jeg længden af linien "linein", man kan jo ikke bruge
> sizeof på dynamisk allokerede arrays?

linein skal du allokere til at være stor nok!
Længden af strengen finder du vha. strlen - strengen er jo
nul-termineret.
> 
> Findes der forøvrigt lettere metoder til at analysere strenge?

Ja, fx. kan du bruge en leksikal scanner som flex, den kan selv læse fra
input - læs man-siden.
 

/Egon


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 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] *