Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C coding in Linux shell script Instructions: The purpose of this assignment is to familiarize you with system calls read( ), write( ), lseek (

C coding in Linux shell script

Instructions:

The purpose of this assignment is to familiarize you with system calls read( ), write( ), lseek ( ) and library functions fgets( ), scanf( ), and strlen( ). Complete the code in db.c as specified by the comments in the file. You need to complete the functions put (int), and get(int). Do not modify a8.c and sr.h.

Compile the source files using the following command: gcc -Wall -o db db.c a8.c

Then execute by typing ./db str

If str is not present open( ) in a8.c will create. If 'str' exists, then it will not be truncated.

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

Sample input/output of the program: Enter your choice (1 to put, 2 to get, 0 to quit) : 1 Enter the student name: Abdul Wallace Enter the student id: 47678 Enter the record index: 0 Enter your choice (1 to put, 2 to get, 0 to quit) : 1 Enter the student name: Lakshmi Wong Enter the student id: 34980 Enter the record index: 9 Enter your choice (1 to put, 2 to get, 0 to quit) : 2 Enter the record index: 9 Student name Lakshmi Wong Student id: 34980 Record index: 9 Enter your choice (1 to put, 2 to get, 0 to quit) : 0 Another run: { babbage: /3377/ io }./ db str Enter your choice (1 to put, 2 to get, 0 to quit) : 2 Enter the record index: 9 Student name Lakshmi Wong Student id: 34980 Record index: 9 Enter your choice (1 to put, 2 to get, 0 to quit) : 0 glven codes: [ db.c] needs to be completed \#include // for int32_t \#include // for assert \#include "sr.h" /// include files are not complete. include the appropriate files // Get all the fields of student record sr from stdin // Put the record in the appropriate offset of the file described by fd void put(int32_t fd) \{ sr s; printf("Enter the student name: "); char str1[10]; scanf("\%s", str1); printF("Entered name: \%s ", str1); // WRITE THE CODE to read the name from stdin // store it in s. name // use fgets() // fgets doesnt remove newline. replace ' with ' ' in s.name. strlen() will be useful printf("Enter the student id: "); // // WRITE THE CODE to read student id from stdin // store it in s.sid printf("Enter the record index: "); // // WRITE THE CODE to read record index from stdin // store it in s.index // WRITE THE CODE to seek to the appropriate offset in fd (1seek(), sizeof() will be useful) // WRITE THE CODE to write record s to fd 3 // read the student record stored at position index in fd void get(int32_t fd) \{ sr s; int32_t index; printf("Enter the record index: "); // // WRITE THE CODE to get record index from stdin and store in it index // WRITE THE CODE to seek to the appropriate offset in fd // WRITE THE CODE to read record s from fd printf("Student name \%s In", s.name); printf("Student id: Kd ", s.sid); printf("Record index: \%d (n", s.index); assert (index = s. inde x ); [a8.c] do not edit \#include inttypes.h // for SCNd32, PRId32 \#include // for EINTR \#include sunistd.hs // for read, close extern void put(int32_t); extern void get(int32_t); // Print out the usage of the program and exit. void Usage(void); // create a database of students record // write a new record // read an existing record int. main(int argc, char** argv) \{ int32_t fd int8_t choice, flag; // input checking if (argcI=2 ) Usage (); // open input file fd=open(argv[1], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); if (fd=1){ perror("open failed"); exit(EXIT_FAILURE); \} flag=1 while (flag) f printf("Enter your choice ( 1 to put, 2 to get, to quit): "); scanf("\%" SCNd8 "\%*c", \&choice); switch (choice) \{ case 1: put (fd) break; case 2: get (fd); break; default: flag=0; 3 \} close (fd); return EXIT_SUCCESS; 3 void Usage (void) \{ fprintf(stderr, "Usage: ./a8 filename (n"); exit(EXIT_FAILURE); 3 [sr. h] do not edit \#define MAX_NAME_LENGTH 30 typedef struct studentRecord char name[MAX_NANE_LENGTH]; // student name int32_t sid; // id of the student int32_t index; // index of this record in the data base file J sri; // format of the data base file (not a text file). // Not all records will have valid data / record with index record with index 1 record with index n */ Please help with explanations

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago