[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] initalisering i c



>
> Uha hvad med overløb hvis du får en meget lang linie ?
> Der bør være en test for max. størrelse af input linie.
> Og egentlig kan det kodes så du ikke behøver at initialisere hele
> strengen.
> Du glemmer egentlig blot at afslutte med "0".
>
> enum { MaxLine=50};
> char line[MaxLine];
> char c;
>
> y=0;
> do {
>  read (fd, &ch, 1);
>  line[y] = ch;
>  y++;
> } while ( ch != '\n' && y<MaxLine-1) {
> line[y]= '\0';
>
> Og egentlig bør du teste at read() overhovedet læste 1 tegn
> og ikke gav fejl eller at forbindelsen er afsluttet.
>

...f.eks....

enum { MaxLine = 50 };
char line[MaxLine];
char ch = 0;

memset(line,0,Maxline);
while ( read( fd, &ch, 1)>0 && ch!='\n' && y<Maxline-1 ) {
	line[y++] = ch;
}


/Jesper



 
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] *