Key:
void str_get_middlle( char *result, char *str, int start, int end ){ int i; int c; for( str += start, i = 0; (start + i ) <= end && (*result++ = *str++) != ‘\0‘; i++ ){ ; } }
For example:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define STR_LEN 10 6 7 void str_get_middlle( char *result, char *str, int start, int end ); 8 9 int main( int argc, char *argv[] ) 10 { 11 FILE *fp = NULL; 12 char *file_path = NULL; 13 char str[STR_LEN] = "", result[ STR_LEN ] = ""; 14 15 if( argc == 1){ 16 printf( "Too few augument(s)\n" ); 17 exit( EXIT_FAILURE ); 18 } 19 20 file_path = *(++argv); 21 22 if( (fp = fopen( file_path , "r" )) == NULL ){ 23 printf( "Opening file error! path:%s\n", file_path ); 24 exit( EXIT_FAILURE ); 25 } 26 else{ 27 printf( "Opening file [%s] was successfully!\n", file_path ); 28 } 29 30 31 while( fgets( str, sizeof( str ),fp) != NULL ){ 32 // printf( "%s", str ); 33 str_get_middlle( result, str, 0, 1); 34 printf( "%s-", result ); 35 36 str_get_middlle( result, str, 2, 3); 37 printf( "%s-", result ); 38 39 str_get_middlle( result, str, 4, 5); 40 printf( "%s\n", result ); 41 } 42 43 return 0; 44 }
Means: 00112233 >> 00-11-22-33. This code can formt mac address.
时间: 2024-09-27 01:03:19