Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

File line select program Write a program that accepts the name of a file and ena

ID: 3877393 • Letter: F

Question

File line select program Write a program that accepts the name of a file and enables the user to view different lines of text by interactively inputting line numbers. After reading the file into a buffer, the program should repeatedly prompt the user for a line number and then display the appropriate line of text. You can assume that the file has less than 100 lines and that the total number of characters in the file is less than 8192 2. creat create an empty ile (truncate)1O-CRE unlink open close dup read write remove a file open a file for reading/writing close a previously open file duplicate an existing file descriptor extract info from a file put info into a file seek move to specified location in afile

Explanation / Answer

Here i am using GCC compiler. You can use any one of the compiler to execute this code.

File: prog1.c

#include<fcntl.h>
#include<sys/stat.h>
#include <string.h>
int main()
{

   char str1[80] = "Enter the file name that you want to read : ";
   char filename[21], buffer[8124],ch[3], in[5];
   int num, i, count;
   write(1, str1, strlen(str1));                          #Here displaying prompt to user to enter filename or else you can intialize the filename without reading from input
   read(0, filename, 21);                                     #Reading the filename from stdin
    filename[strlen(filename)-1] = '';                           #while reading from stdin filename will store ' ' character also, renmoving that character by storing NULL
   int io1 = open(filename, O_RDONLY);             #Opening file in read mode
   if(io1 == -1)                                                 #Checking if file is opened or not
   {
       write(1, "Cannot open file ", strlen("Cannot open file "));
       return 1;
   }
   write(1, "File opened successfully ", strlen("File opened successfully "));       #Displaying message after opening file
   int n = read(io1, buffer, 8124);          #Reading entire file into buffer
   do
   {
       count = 0;
       char str3[80] = "Enter the line number that you want to read: ";
       write(1, str3, strlen(str3));                            #Displaying prompt to user to enter the line number
       read(0, in, 4);                                             #Reading line number
       num = atoi(in);                                            #While reading integers also will store as string using atoi function we can convert string to integer
       if(num > 100)                                             #Here 100 i am taking as a total no. of lines if you want you can mopdify this value by finding the total number of lines
       {
           write(1, "Line number is invalid, Please enter valid line number ", strlen("Line number is invalid, Please enter valid line number "));
           continue;
       }
       for(i=0; i<=n; i++)
       {
           if(buffer[i] == ' ')                   #From buffer reading character by character and comparing with newline
           {
               count++;                         #If it is newline incrementing 1 to count variable
           }
           if(count == num-1)               #Here i am comparing with entered number -1 because when we enter 3 if count is 3 means we completed reading of 3 lines then i will point to 4 th line so decrementing to 2 means completed 2 lines and i pointing to starting of 3 rd line
           {  
               i++;                          #i will point to so moving one byte that means starting of next line
               while(buffer[i] != ' ')        #Loop will repeat till the next line encountered
               {
                   write(1, &buffer[i], 1);        #Writing character by character to stdout till the last line
                   i++;
               }
               write(1, " ", 1);                 #Printing newline at the end of line text
               break;
           }
          
       }
       write(1,"Do you want to continue?(y/n) ", strlen("Do you want to continue?(y/n) ")); #Suppose if you want to see another line text press y otherwise n or any character
       read(0,&ch, 2);              #Here reading 2 characters beacuse one is entered character and 2nd one is
   }while(ch[0] == 'y');            #If it is equal to 'y' then again it will ask for line number otherwise it will quit from the program

   return ;
}

OUTPUT:

Enter the file name that you want to read :
file1.txt
File opened successfully
Enter the line number that you want to read:
5
This is line5
Do you want to continue?(y/n)   y
Enter the line number that you want to read:
8
This is line8
Do you want to continue?(y/n)   n

Thank you,

Hoping you understand. In case of any queries comment below.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote