Question

Please answer the following C question: There is a documented prototype for a function called get_a_line...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
1) Develop a C++ function that determines the average value of an array of type double...
1) Develop a C++ function that determines the average value of an array of type double elements double GetAverage(double array[], int size) The function should accept as input an array of double values The function should accept as input the number of elements in the array of double values The function should return a double value which is the array's average value 2) Develop a C++ function that determines the variance of an array of type double elements double GetVariance(double...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Question asks to use if statements to test if the value entered is an A. If...
Question asks to use if statements to test if the value entered is an A. If true print "The value of an A is 4.0" I'm struggling to get it to give the right results using char variable which the questions says to use. C programming. int main() { char Grade = 'X'; // Declares a character type variable named Grade printf("Enter a grade\t"); // Prompts for Grade scanf("%c", &Grade); // Inputs Grade printf("Grade is: \t%c\n", Grade); // Prints the...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy....
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy. Disk blocks are numbered consecutively from the beginning of the file with the first block numbered as 0. Assume that blocks are 4096 bytes in size. Use the supplied C++ files to implement your LRU Buffer Pool based on the instructions below. • Implement a BufferBlock class using the supplied BufferBlockADT.h o Your Buffer Block must inherit BufferBlockADT or you will not get credit...