1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isFree ( FILE* file, char* tab ) ;
int main ()
{
FILE* f ;
int num = 3306 ;
char buf[5] ;
itoa ( num, buf, 10 ) ;
char* fileName = "usedPorts.txt" ;
f = fopen ( fileName, "r" ) ;
if ( f == NULL )
return 0 ;
while ( isFree ( f, buf ) != 0 )
{
num = num + 1 ;
itoa ( num, buf, 10 ) ;
}
fclose ( f ) ;
printf ( "%d", num ) ;
return num ;
}
int isFree ( FILE* file, char* tab )
{
char c ;
int found = 0 ;
int pos = 0 ;
int length ;
length = strlen ( tab ) ;
while( fread ( &c, 1, 1, file )>0 && !found )
{
if ( c == tab[pos] )
{
pos++ ;
}
else
{
if ( pos != 0 )
{
// On doit rembobiner !
fseek ( file, -pos ,SEEK_CUR );
pos = 0 ;
}
}
found = ( pos == length ) ;
}
return found ;
} |
Partager