Since you are reusing them, it would be better to avoid magic numbers with some named constants, e.g.,
Code:
#define ITEM_MIN_LEN 3
#define ITEM_MAX_LEN 17
#define NUM_ROOMS 9
Then, you can make use of these to ensure that your definitions don't get out of sync, and that you are specifying a field with for %s with fscanf so as to avoid buffer overflow. For example:
Code:
int encoding = 0; //variable to represent whats in the room 0-8 (9 different items)
char item[ITEM_MAX_LEN + 1];// item
char itemArr[NUM_ROOMS][ITEM_MAX_LEN + 1]; //array of items in the room
int numItems[NUM_ROOMS]; //number of item in each room
/* format not expected to exceed field width of 5 digits, e.g., "%*i %10000s" */
char format[12];
sprintf(format, "%%*i %%%ds", ITEM_MAX_LEN);
while (encoding < NUM_ROOMS && fscanf(input, format, item) == 1){
if (strlen(item) >= ITEM_MIN_LEN){
strcpy(itemArr[encoding], item);
printf("%s\n", itemArr[encoding]);
encoding++;
}
encoding++;
while((le = fgetc(input)) != '\n' && le != EOF);
}
if (encoding > 0){
printf("%s\n", itemArr[0]);
}
By the way, why do you increment encoding twice when the item string length is greater than the minimum length? This could be a problem where say, the first line of input is too short, upon which itemArr[0] is left uninitialised, but you have no way to tell. Notice that I added a check for encoding > 0 because it is also possible that the first line of input is simply invalid, upon which the loop will terminate before having given itemArr[0] an initial value. It is also possible that there are more lines of valid input with strings within range than there are rooms, hence I added a check for encoding < NUM_ROOMS into the loop condition.