Please answer the following C question:
There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc.
#include <stdio.h>
#include <string.h>
#define BUFFER_ARRAY_SIZE 10
int get_a_line(char *s, int size, FILE *stream);
// Does what fgets does, using repeated calls to fgetc, but
// provides a more useful return value than fgets does.
//
// REQUIRES
// size > 1.
// s points to the start of an array of at least size bytes.
// stream is open for input.
// PROMISES
// Return value is EOF if input error occurred.
// Otherwise, return value gives the index of the '\0' that
// terminates the string in the array.
int main(void)
{
char buffer[BUFFER_ARRAY_SIZE];
int retval;
printf("Enter a line of text:\n");
retval = get_a_line(buffer, BUFFER_ARRAY_SIZE, stdin);
printf("\nString in buffer is \"\%s\"\n", buffer);
if (retval == EOF)
printf("Return value from get_a_line was EOF.\n");
else
printf("Return value from get_a_line was %d.\n", retval);
return 0;
}
Then,
You can use the given main function to test your code. You should run the executable several times to check different cases:
- Type Ctrl-D in response to the prompt for input. get_a_line should fail to read any characters, put a ’\0’ character at the beginning of the array, and return EOF.
- Type abc and then Ctrl-D twice in response to the prompt for input. That input generates a failure after ’a’, ’b’, and ’c’ have been read.
- Type abc and then Enter in response to the prompt for input. In that situation, all three letters followed by a newline should get into the array.
- Type abcdefgh and then Enter in response to the prompt for input. In that situation, all eight letters followed by a newline should get into the array.
- Type abcdefghi and then Enter in response to the prompt for input. In that situation, all nine letters should get into the array, but the newline won’t.
- Type abcdefghijklmnop and then Enter in response to the prompt for input. In that situation, the first nine letters get into the array but not the other letters or a newline
Complete code in C:-
#include <stdio.h>
#include <string.h>
#define BUFFER_ARRAY_SIZE 10
int get_a_line(char *s, int size, FILE *stream);
// Does what fgets does, using repeated calls to fgetc, but
// provides a more useful return value than fgets does.
//
// REQUIRES
// size > 1.
// s points to the start of an array of at least size bytes.
// stream is open for input.
// PROMISES
// Return value is EOF if input error occurred.
// Otherwise, return value gives the index of the '\0' that
// terminates the string in the array.
int main(void)
{
char buffer[BUFFER_ARRAY_SIZE];
int retval;
printf("Enter a line of text:\n");
retval = get_a_line(buffer, BUFFER_ARRAY_SIZE,
stdin);
printf("\nString in buffer is \"%s\"\n",
buffer);
if (retval == EOF)
printf("Return value from
get_a_line was EOF.\n");
else
printf("Return value from
get_a_line was %d.\n", retval);
return 0;
}
// Implementation of 'get_a_line' function
int get_a_line(char *s, int size, FILE *stream) {
// Variable 'i' will keep track of index in array
's[]'.
int i = 0;
char ch;
// Reading using 'fgetc()', it will treat 'CTRL+d' as
'EOF'
while(i < 9 && (ch = fgetc(stream)) != EOF)
{
s[i] = (char)ch;
i++;
// If user hit the enter, break the
loop.
if((char)ch == '\n') {
break;
}
}
// Put '\0' in the last last position of 's[]',
s[i] = '\0';
// If user entered 'CTRL+d' return EOF.
if(i == 0) {
return EOF;
}
return i;
}
Screenshot of output:-
Get Answers For Free
Most questions answered within 1 hours.