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"); }

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!