C Programming My program is not passing tests on an online g

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.

update: i just realized i havent been Setting each row of lines to contain one line of the file. How would I go about doing that?

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:

#include

#ifndef READ_LINES
#define READ_LINES

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

#endif

.c file:

#include \"read_lines.h\"
#include
#include

void read_lines(FILE* fp, char*** lines, int* num_lines) {
FILE* fp1;
int num_char = 0;
   char c;
  
   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(fp1)) != 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);
}

some online grader output:

Test FAILED.

Reason: Your output contains FEWER words than the solution.

Way program was called: ./read_lines.out /autograder/source/Tests/Files/tabs.txt

Input provided:

YOUR output contains 15 FEWER words than the solution.

Last 10 of YOUR answer: Last matching part of the SOLUTION + 10 more words tabs between the characters 2.

YOUR complete output:

Complete SOLUTION: 1. This   file   has   some   tabs   between       the    characters      

2. and here are some spaces

Test FAILED.

Reason: Your output contains FEWER words than the solution.

Way program was called: ./read_lines.out /autograder/source/Tests/Files/extra_lines.txt

Input provided:

YOUR output contains 17 FEWER words than the solution.

Last 10 of YOUR answer: Last matching part of the SOLUTION + 10 more words lines after this

YOUR complete output:

Complete SOLUTION: 1. There are some

2. extra new lines after this line

3.

4.

5.

6.

7.

8.

Solution

Hey I didnt get completely whats your bug. After looking at the code I can tell that it is related to program for counting number of characters and lines.

This is the code/block from your program which we are interested in:

while ((c = getc(fp1)) != 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;
}
}

instead of placing two ifs make it if/else conditions:

while ((c = getc(fp1)) != 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;
}
else if (c == \'\ \') { //if there is a change in line
++num_lines;
}
}

Now this has to work right, check once and post comments if you have any problems.

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 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 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,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site