I am writing a C program that should print the Cartesian product of n sets, where the elements of each set are the line of text in one file, and the names of the n files are passed as command-line arguments. So far I managed to read every line into matrix of strings. However I cannot wrap my head around how to write the algorithm for printing the product.
Here is what I have so far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LSIZ 128
#define RSIZ 10
int main(int argc, char *argv[])
{
char input[LSIZ];
int n = argc;
char *sets[argc - 1][RSIZ];
int i = 0;
int j = 0;
int y = 1;
FILE *file = NULL;
if (argc == 1)
{
printf("nofiles");
}
else
{
while (--argc > 0)
{
if ((file = fopen(argv[y], "r")) == NULL)
{
printf("cat: failed to open %s\n", *argv);
return 1;
}
else
{
for (j = 0; j < RSIZ && fgets(input, sizeof(input), file); ++j)
{
int lineLen = strlen(input) + 1;
sets[i][j] = strncpy(malloc(lineLen), input, lineLen);
}
fclose(file);
j = 0;
}
i++;
y++;
}
}
return 0;
}