Reputation: 193
I have a program which (recursively) creates combinations of varying length from the letters of the alphabet and wishes to append them to a file:
#include <stdio.h>
#include <stdlib.h>
void combinationUtil(char arr[], char data[], int start, int end,
int index, int r);
void printCombination(char arr[], int n, int r)
{
char data[r];
combinationUtil(arr, data, 0, n-1, 0, r);
}
void combinationUtil(char arr[], char data[], int start, int end,
int index, int r)
{
FILE *f = fopen("file.txt", "a");
const char *eol = "\n";
if (f == NULL)
{
printf("Error in opening file!\n");
exit(1);
}
if (index == r)
{
for (int j=0; j<r; j++)
{
fprintf(f, "%c", data[j]);
printf("%c", data[j]);
}
fprintf(f, "%s", eol);
printf("\n");
fclose(f);
return;
}
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int r=1; r<=14; r++)
{
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}
}
This program complies without any warnings or errors. I wish to write combinations between length 1 to length 14 to my file (and yes, I am aware that this will take a long time and will make the resultant file large).
Nevertheless, there comes a point where the program simply chokes and prints:
Error in opening file!
which means that the pointer has gained a NULL
value. Why does this happen and how can I fix it?
Upvotes: 0
Views: 241
Reputation: 26800
Windows specifies some limits on the number of files that can be opened at once:
The C run-time libraries have a 512 limit for the number of files that can be open at any one time. Attempting to open more than the maximum number of file descriptors or file streams causes program failure. Use _setmaxstdio to change this number.
And if you are using Linux, there is a limit on the number of files that can be opened by a process (which you can find using the following command):
ulimit -n (n is the the maximum number of open file descriptors, most systems do not allow this value to be set)
A root user can try to change the maximum of the open files count per process and per system.
echo noOfFilesIwantToOpen > /proc/sys/fs/file-max
Upvotes: 2
Reputation: 193
Sigh...
#include <stdio.h>
#include <stdlib.h>
void combinationUtil(char arr[], char data[], int start, int end,
int index, int r, FILE *f);
void printCombination(char arr[], int n, int r, FILE *f)
{
char data[r];
combinationUtil(arr, data, 0, n-1, 0, r, f);
}
void combinationUtil(char arr[], char data[], int start, int end,
int index, int r, FILE *f)
{
const char *eol = "\n";
if (index == r)
{
for (int j=0; j<r; j++)
{
fprintf(f, "%c", data[j]);
printf("%c", data[j]);
}
fprintf(f, "%s", eol);
printf("\n");
return;
}
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r, f);
}
}
int main()
{
FILE *f = fopen("file.txt", "a");
if (f == NULL)
{
printf("Error in opening file!\n");
exit(1);
}
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int r=1; r<=14; r++)
{
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r, f);
}
fclose(f);
}
Upvotes: 0