Question
Develop a simple version of the I/O library function fgets() using Unix I/O system calls. Here is the description of fgets() from the manual: Prototype:
Develop a simple version of the I/O library function fgets() using Unix I/O system calls. Here is the description of fgets() from the manual: Prototype: char *fgets(char *s, int size, FILE *stream) fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte is stored after the last character in the buffer. fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read. Below is a test driver for the function myFgets() to be implemented. #include #include #include #include #ifndef _IOEOF #define _IOEOF 0020 #endif #ifndef _IOERR #define _IOERR 0040 #endif char *myFgets(char *buf, int size, FILE *fp); int main(int argc, char *argv[]){ FILE *fp; char buf[100], int cnt=0; if(argc == 1) // reading from keyboard fp=stdin; else if((fp = fopen(argv[1], "r")) == NULL){ perror("Could not open input file"); exit(1); } while(myFgets(buf, 100, fp) != NULL){ fputs(buf, stdout); cnt =+ strlen(buf); } if(feof(fp)) printf("The file size is %d ", cnt); else perror("Reading not completed"); } The function myFgets() must: return NULL when an error or the end of file has occured, update the _flag of the FILE object accordingly(use the constants _IOEOF and _IOERR). obtain the file Unix descriptor using the function fileno(FILE *), e.g., fd=fileno(fp);.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started