Tuesday, February 19, 2008

Finish the Network programming task with top speed ~ Happy

First time taste the happiness of fast programming.

some tips for C:

1. when read sth from FILE

Do NOT use
while(!feof(fpt))

that will lead to some troubles.

Instead, try with

char tmp[size];
while(fgets(tmp,size,fpt)!=NULL)


2. How to transfer int to string

my way:

char buf [Big Enough];
int num;
sprintf(buf,"%d\0", num);

the format can be anything, be careful with the buffer overflow.


3. from string to int

#include

int main(void)
{
const char text[] = "097102099105110";
int i, value[5];
if ( sscanf(text, "%3d%3d%3d%3d%3d", &value[0],
&value[1], &value[2], &value[3], &value[4]) == 5 )
{
for ( i = 0; i < 5; ++i )
{
printf("value[%d] = %d\n", i, value[i]);
}
}
return 0;
}

output
value[0] = 97
value[1] = 102
value[2] = 99
value[3] = 105
value[4] = 110

4. How to get zero paddings with printf:

printf("%05d",int);

if int = 1; output will be like 00001

No comments: