I need help in creating a function based on the program below.  The program below prints out the particular line in a text file . I want to modify this program to a function whose parameter is the name of the particular text file and it returns the string present on the specific line. I however also want the particular line number to be randomly generated ranging from 1 till 50. Restrictions are that I can't use break,exit,continue to get out of a loop. #include #include // constants for size of char arrays to store filename, the line from the file #define FILENAME_SIZE 1024 #define MAX_LINE 2048 int main() {  // file pointer will be used to open/read the file   FILE *file;   char filename[FILENAME_SIZE];   char buffer[MAX_LINE];   int read_line = 0;   printf("File: ");   scanf("%s", filename);   printf("Read Line: ");   scanf("%d", &read_line);   file = fopen(filename, "r");   if (file == NULL)   {     printf("Error opening file.\n");     return 1;   }   bool keep_reading = true;   int current_line = 1;   do    {     fgets(buffer, MAX_LINE, file);     if (feof(file))     {       keep_reading = false;       printf("File %d lines.\n", current_line-1);       printf("Couldn't find line %d.\n", read_line);     }     else if (current_line == read_line)     {       keep_reading = false;       printf("Line:\n%s", buffer);     }    current_line++;   } while (keep_reading);   fclose(file);   return 0; }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help in creating a function based on the program below.  The program below prints out the particular line in a text file . I want to modify this program to a function whose parameter is the name of the particular text file and it returns the string present on the specific line. I however also want the particular line number to be randomly generated ranging from 1 till 50. Restrictions are that I can't use break,exit,continue to get out of a loop.

#include <stdio.h>
#include <stdbool.h>

// constants for size of char arrays to store filename, the line from the file
#define FILENAME_SIZE 1024
#define MAX_LINE 2048

int main()
{
 // file pointer will be used to open/read the file
  FILE *file;


  char filename[FILENAME_SIZE];
  char buffer[MAX_LINE];


  int read_line = 0;


  printf("File: ");
  scanf("%s", filename);

  printf("Read Line: ");
  scanf("%d", &read_line);

  file = fopen(filename, "r");
  if (file == NULL)
  {
    printf("Error opening file.\n");
    return 1;
  }

  bool keep_reading = true;
  int current_line = 1;
  do 
  {
    fgets(buffer, MAX_LINE, file);
    if (feof(file))
    {
      keep_reading = false;
      printf("File %d lines.\n", current_line-1);
      printf("Couldn't find line %d.\n", read_line);
    }

    else if (current_line == read_line)
    {
      keep_reading = false;
      printf("Line:\n%s", buffer);
    }

   current_line++;

  } while (keep_reading);

  fclose(file);
  return 0;

}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY