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

C Programming My program is not passing tests on an online grader. Any help conc

ID: 3774218 • Letter: C

Question

C Programming

My program is not passing tests on an online grader. Any help concerning what I am doing wrong is appreciated! I have included my homework prompt, .c and .h file, and the online grader output.

Prompt:

This function has the following definition:
1. void read_lines(FILE* fp, char*** lines, int* num_lines).

This function should read all of the lines contained within fp and

Set each row of lines to contain one line of the file.

Set num_lines to be equal to the number of lines that were in the file

If the file is empty lines should be set to NULL and num_lines to 0.

You only need to submit read_lines.c and read_lines.h

read_lines.h must contain at least the definition for read_lines but it is perfectly ok if there

are more

.h file:

#ifndef READ_LINES
#define READ_LINES

void read_lines(FILE* fp, char*** lines, int* num_lines);

#endif

.c file:

#include "read_lines.h"
#include <stdio.h>
#include <stdlib.h>

void read_lines(FILE* fp, char*** lines, int* num_lines) {
   int num_char = 0;
   char c;
  
   fp = fopen(lines[1], "r"); //open and read file

   if (fp == NULL) { //if file is empty
lines = NULL;
       num_lines = 0;
exit(0);
}

if (fp) { //if file opens
while ((c = getc(fp)) != EOF) { //getc gets the next character
if (c != ' ' && c != ' ') { //add one to num_char if there is no space or change in line
++num_char;
}
if (c == ' ') { //if there is a change in line
++num_lines;
}
}
if (num_char > 0) {
++num_lines; //last line
}
}
  
rewind(fp); //moves read pointer to beginning of file
  
(*lines) = (char**)malloc(num_lines * sizeof(char*))
  
   fclose(fp);
}

grader output:

Compiler failed with nonzero exit code: 2

Compiler output: gcc -g -Wall -Werror -c -o read_lines.o read_lines.c

Makefile:8: recipe for target 'read_lines.o' failed

.

Compiler error: In file included from read_lines.c:1:0:

read_lines.h:4:17: error: unknown type name 'FILE'

void read_lines(FILE* fp, char*** lines, int* num_lines);

^

read_lines.c: In function 'read_lines':

read_lines.c:10:16: error: passing argument 1 of 'fopen' from incompatible pointer type [-Werror=incompatible-pointer-types]

fp = fopen(lines[1], "r"); //open and read file

^

In file included from read_lines.c:3:0:

/usr/include/stdio.h:272:14: note: expected 'const char * restrict' but argument is of type 'char **'

extern FILE *fopen (const char *__restrict __filename,

^

read_lines.c:34:41: error: invalid operands to binary * (have 'int *' and 'long unsigned int')

(*lines) = (char**)malloc(num_lines * sizeof(char*))

^

read_lines.c:36:5: error: expected ';' before 'fclose'

fclose(fp);

^

cc1: all warnings being treated as errors

make: *** [read_lines.o] Error 1

Explanation / Answer

Hi,

1) add #include <stdio.h> to your read_lines.h file until you don't include the responsible directory header file cannot read about the inbuilt FILE type.

2)After fixing the issues shadow operator error is coming extra that is because the declaration of the function and variable inside are having same name so you can change the to

FILE * fp1 = fopen(lines[1], "r");

3) you can not directly write fp first you should define a varable for fp of which type it belongs to so

FILE * fp1;
fp1 = fopen(lines[1], "r"); would be much clear and appropriate.

4)As per the declaration i came to know that we are passing a triple pointer value lines to the function

i.e., char*** lines so, we in that we are giving the file name as input while you read the input you have used lines[1] which will be of type char**(double pointer )and for fopen function we just need to give a file pointer of type char*(single pointer)

so how will you access means lines[1][0]

( that is what the line " /usr/include/stdio.h:272:14: note: expected 'const char * restrict' but argument is of type 'char **' extern FILE *fopen (const char *__restrict __filename," means : )

5)Final touch last line you need to modify a little bit like this:

lines = (char***) malloc((*num_lines)*sizeof(char*));

Here (*lines) = (char**) you can directly convert it to triple pointer

next num_lines is a pointer and we should &want to multiply the it the integer value of num_lines which will be (*num_lines)

DON'T forget the semicolon at the end of any line in a c++ program

Any further clarification needed please comment below

total code:

.h file:

#ifndef READ_LINES
#define READ_LINES
#include <stdio.h>
void read_lines(FILE* fp, char*** lines, int* num_lines);
#endif

.cpp file:

#include "hello.h"
#include <stdio.h>
#include <stdlib.h>

void read_lines(FILE* fp, char*** lines, int* num_lines) {
int num_char = 0;
char c;
FILE * fp1;
fp1 = fopen(lines[1][0], "r"); //open and read file
if (fp1 == NULL) { //if file is empty
lines = NULL;
num_lines = 0;
exit(0);
}
if (fp1) { //if file opens
while ((c = getc(fp)) != EOF) { //getc gets the next character
if (c != ' ' && c != ' ') { //add one to num_char if there is no space or change in line
++num_char;
}
if (c == ' ') { //if there is a change in line
++num_lines;
}
}
if (num_char > 0) {
++num_lines; //last line
}
}
  
rewind(fp1); //moves read pointer to beginning of file
  
lines = (char***) malloc((*num_lines)*sizeof(char*));
  
fclose(fp1);
}