Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <sys/wait.h> #include <sys/stat.h> void getInputs(char *, char **, char

ID: 3557804 • Letter: #

Question

#include <sys/wait.h>
#include <sys/stat.h>

void getInputs(char *, char **, char **);

void getInputs(char *str, char **inputs, char **tests) {
   FILE *fp = fopen(str, "r");
   int temp;
   if(fp == NULL)
      printf("Cannot open the file ");
   else {
      while ((temp = fgetc(fp)) != EOF)
         printf("%c ", temp);
         if(temp == 'T') {
            while (fgets(*inputs, 100, fp) != " ") {
               printf("Line %s", *inputs);
            }
         }       
   }
}
int main(int argc, char **argv) {
   char **inputs, **tests;
   argv++;
    if (*argv == NULL) {
       printf("usage: Calculon <suite definition file> ");
    }
    else {
       getInputs(*argv, inputs, tests);
    }

    return 0;
}

Explanation / Answer

#include <sys/wait.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

void getInputs(char *, char **, char **);
void getInputs(char *str, char **inputs, char **tests) {
char buf[100];
FILE *fp = fopen(str, "r");
int temp;
inputs[0] = NULL;
tests[0] = NULL;

if(fp == NULL)
printf("Cannot open the file ");
else {
while ((temp = fgetc(fp)) != EOF) {
/* printf("%c ", temp); */
if(temp == 'T') {
*tests = (char*)malloc(100);
fgets(*tests, 100, fp);
tests++;
*tests = NULL;
}
else if (temp == 'P') {
*inputs = (char*)malloc(100);
fgets(*inputs, 100, fp);
inputs++;
*inputs = NULL;
}
else {
fgets(buf, 100, fp);
}
}
}
}

int main(int argc, char **argv) {
char *inputs[200], *tests[200];
int i;
argv++;
if (*argv == NULL) {
printf("usage: Calculon <suite definition file> ");
}
else {
getInputs(*argv, inputs, tests);

/* output input and tests */
for(i = 0; inputs[i]!=NULL; i++) {
printf("input: %s ", inputs[i]);
}
for(i = 0; tests[i]!=NULL; i++) {
printf("test: %s ", tests[i]);
}
}
return 0;
}