In: Computer Science
C program only
There is a documented prototype for a function called get_a_line in the .c file. Write a definition for get_a_line—the only function called from that definition should be fgetc. 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.
Hint: Writing get_a_line requires a lot of thought, but not a lot of code. It can be done in 20 or fewer rather short lines of code.
C Code:
// test-get_a_line4D.c
#include <stdio.h>
#include <string.h>
// Again, in a practical program, this is a ridiculously small
size
// for an array that is supposed to hold a line of text. But
it's
// convenient for testing purposes.
#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;
}
code in c (code to copy)
#include <stdio.h>
#include <string.h>
// Again, in a practical program, this is a ridiculously small size
// for an array that is supposed to hold a line of text. But it's
// convenient for testing purposes.
#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;
}
int get_a_line(char *s, int size, FILE *stream){
// declare a counter for the s string and initialize with 0
int k=0;
//read first character
char ch = fgetc(stream);
//check if valid line
if(ch==EOF||ch=='\n'){
s[0]='\0';
return EOF;
}
//loop until end of line
while(k<size-1&&ch!=EOF&&ch!='\n'){
s[k++]=ch;
ch = fgetc(stream);
}
s[k]='\0';
return k;
}
code screenshot
output screenshot #1
output screenshot #2