#include #include #include #include #define MAX_FILES 50 int openArrayFile( FILE** pFilesArray, const int index, const char* fileName, const char* openMode ) { char* pHelloWorld = "Hello, World!\n"; int rc = (index >= MAX_FILES ) ? -1 : 0; if( !rc ) { pFilesArray[index] = fopen( fileName, openMode ); if( !pFilesArray[index] ) { rc = errno; } fwrite( pHelloWorld, strlen( pHelloWorld ), 1, pFilesArray[index] ); } return rc; } int closeArrayFile( FILE** pFilesArray, const int index ) { int rc = (index >= MAX_FILES ) ? -1 : 0; if( !rc ) { if( !pFilesArray[index] ) { rc = ERANGE; } else { rc = fclose( pFilesArray[index] ); if( rc ) { rc = errno; } else { pFilesArray[index] = 0; } } } return rc; } int main() { int rc = 0; int i = 0; char filenameBase[16] = {0}; FILE** pFilesArray = (FILE**)calloc( sizeof( FILE*), MAX_FILES ); if( pFilesArray != NULL ) { for( i = 1; i < 1+ MAX_FILES; i++ ) { if( i > 999 ) { rc = ERANGE; break; } else if( i < 10 ) { sprintf( filenameBase, "result00%d", i ); } else if( i < 100 ) { sprintf( filenameBase, "result0%d", i ); } else { sprintf( filenameBase, "result%d", i ); } rc = openArrayFile( pFilesArray, -1 + i, filenameBase, "w" ); if( rc ) { break; } else { rc = closeArrayFile( pFilesArray, -1 + i ); if( rc ) { break; } } } free( pFilesArray ); } else { rc = errno; } return rc; }