Question: Hi, I have a C programming problem. Background Material The Task: The Answer: The completed .c file. Below are files: lab6exD.c: // You may modify

Hi, I have a C programming problem.

Background Material

Hi, I have a C programming problem. Background Material The Task: The

The Task:

Answer: The completed .c file. Below are files: lab6exD.c: // You may

The Answer:

The completed .c file.

Below are files:

lab6exD.c:

// You may modify this incomplete program however you like // to solve the problem given in the lab instructions. #include  #include  #include  // For the purposes of this exercise, let's assume that the maximum // length of a word is 255 characters. #define BUF_SIZE 256 #define FSCANF_CONTROL "%255s" #define FILENAME "the_text.txt" int main(void) { char input_str[BUF_SIZE]; char longest_word[BUF_SIZE]; char shortest_word[BUF_SIZE]; int len, max_len = 0, min_len = BUF_SIZE, count = 0; FILE *fp; int nscan; return 0; }

The_text.txt:

Text file input using the C library The pattern for input is very similar to the pattern for output The basic set of steps is common to most programming languages and libraries and is not specific to C Obtain a file name In C that means get a character string Open the file for input make a connection between the program and the file The connection will allow the program to read character codes from the file Read some characters from the file Close the file break the connection between the program and the file

Read This Second Lecture notes have not discussed using scanf or fscanf to read input as a series of whitespace-separated tokens, so I'll explain how that works here First, here's an example what not to do. Suppose that fp is of type FILE*, and points to a FILE object that has been opened for input... char b [20]; int nscan; nscan- fscanf (fp, "%s" b); // DANGER ! , The problem in the above use of fscanf is that fscanf is not told the size of the array b, and a buffer overflow wil occur if there is a sequence of 20 or more non-whitespace characters in the input stream. This is a safe way to get a string using fscanf char b [20]; int nscan; nscan = fscant(fp, "%19s", b); // will not read more than 19 chars The above function call will consume and discard whitespace until a non-whitespace character is found. Then, usually, it will make a string in the array b by copying consecutive non-whitespace input characters into the array until either a whitespace character is found in the input, or 19 characters have been copied into the array. In either case, nscan will get a value of 1. For example, suppose the input sequence looks like this, where u represents a space: The call to fscanf w put 'A', 'B', and 0' into b[o], b[1], and b[2]. The first character to be read by the next input operation with fp will be the first of the two spaces between B and C in the input stream In the case that fscanf finds nothing but whitespace until the end of a file, it will return EOF. Of course the numbers 19 and 20 in the code above are just examples. In general, the value of n in "%ns" should be one less than the size of the array that is to receive the string, to allow room for a terminating 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!