char* ReadLineFromFileByRandomIndex(char* path) { if(path) { char* result = (char*)calloc(25, sizeof(char)); int linesInFile = CountLinesInFile(path); int randomIndex = RandomNumberInRange(1, linesInFile); FILE* file; fopen_s(&file, path, "r"); int count = 1; while (++count < randomIndex) { fgets(result, 25, file); } fgets(result, 25, file); fclose(file); return(result); } return(""); }
int main( int argc, char *argv[] ) { short i, n[10]; Digit r; DigitList x; srand( (unsigned int) getpid() ); list_New( &x ); /* Zero the frequency array */ for( i=0; i<10; i++ ) n[i] = 0; /* Insert digits, counting frequency as we go */ for( i=0; i<1000; i++ ) { r = RandomNumberInRange( 0, 9 ); n[r]++; list_Add( &x, r ); } /* Display frequencies */ for( i=0; i<10; i++ ) printf( "%d: %d\n", i, n[i] ); #if kDEBUG /* Display raw data if we are debugging */ for( i=1; i<=list_Size( x ); i++ ) printf( "%u ", list_Nth( x, i ) ); printf( "\n" ); #endif /* Zero the frequency array */ for( i=0; i<10; i++ ) n[i] = 0; /* Recompute the frequencies by traversing the list */ for( i=1; i<=list_Size( x ); i++ ) n[ list_Nth( x, i ) ]++; /* Display frequencies again */ for( i=0; i<10; i++ ) printf( "%d: %d\n", i, n[i] ); printf( "\n" ); list_Dispose( &x ); return( 0 ); }
internal_function char* DODSearchThroughNextTags(dod_tag* dodTag, char* tag, char* value, char* subTag = 0, char* subValue = 0) { if(subTag && subValue) { dod_tag* nextDODTag = dodTag; while(nextDODTag) { if(CompareCharPointers(nextDODTag->tag, tag)) { if(CompareCharPointers(nextDODTag->value, value)) { char* result = DODSearchThroughNextTags(nextDODTag->subTag, subTag, subValue); if(result) { return(result); } } } nextDODTag = nextDODTag->nextTag; } } while(dodTag) { if(CompareCharPointers(dodTag->tag, tag)) { if(CompareCharPointers(dodTag->value, value)) { if(dodTag->content) { //NOTE: Getting the contentSize by subtracting 2, we lose the \n and final \0 int randomIndex = RandomNumberInRange(0, dodTag->amountOfContent - 1); int contentSize = CharPointerLength(dodTag->content[randomIndex]) - 2; //NOTE: Copying the content with 1 less of size removes the #. char* result = (char*)calloc(contentSize, sizeof(char)); MemoryCopy(result, dodTag->content[randomIndex], contentSize - 1); return(result); } } } dodTag = dodTag->nextTag; } return(""); }