0 like 0 dislike
58 views
in Computer Science by (1.0m points)
What is the fgets() function.

1 Answer

0 like 0 dislike
by (1.0m points)
The function reads a string from the file pointed to by fp into the memory pointed to by str. The function reads characters from the file until either a newline ('\n') is read or n-1 characters is read or an end of file is encountered, whichever occurs first. After reading the string it appends the null character ('\0') to terminate the string. On success, it returns a pointer to str. On error or end of the file it returns NULL.

The following program demonstrates how to use fgets() function.

In line 7, a structure pointer variable fp of type struct FILE is declared.

In line 8, fopen() function is called with two arguments namely "myfile2.txt" and "r". On success, it returns a pointer to file myfile2.txt and opens the file myfile.txt in read-only mode. On failure or end of file, it returns NULL.

In lines 10-14, if statement is used to test the value of fp. If it is NULL, printf() statement prints the error message and program terminates. Otherwise, the program continues with the statement following the if statement.

In line 16 and 17, two printf() statements string "Testing fgets() function: \n\n" and "Reading contents of myfile.txt: \n\n" to the console.

In lines 19-22, a while loop is used to read the contents of the myfile2.txt. Here is how while loop works:

The function fgets() is called with an argument of 30, so it reads 29 characters from the file, stores them in the array str by appending null character at the end. Then the puts() function is called to display the contents of the str . The puts() function prints the string to the console by converting null character (‘\0’) at the end of the string to a newline (‘\n’) character. This is the reason why newline is printed after each iteration. In the second iteration fgets() encounters end of file after reading 27 characters. The puts() function is called again to print these 27 characters along with newline (‘\n’) at the end. The fgets() function is called again for the third time, but since there are no more characters left to read it returns NULL, the while condition becomes false and control comes out of the while loop.

Related questions

0 like 0 dislike
1 answer 997 views
0 like 0 dislike
1 answer 98 views
asked Apr 5, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 153 views
asked Apr 4, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 59 views
0 like 0 dislike
1 answer 63 views
asked Feb 14, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 41 views
asked Feb 13, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 117 views
asked Jan 7, 2019 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 61 views
0 like 0 dislike
1 answer 68 views
asked Nov 9, 2018 in Computer Science by danish (1.0m points)
0 like 0 dislike
1 answer 56 views
asked Nov 9, 2018 in Computer Science by danish (1.0m points)
Welcome to Free Homework Help, where you can ask questions and receive answers from other members of the community. Anybody can ask a question. Anybody can answer. The best answers are voted up and rise to the top. Join them; it only takes a minute: School, College, University, Academy Free Homework Help

19.4k questions

18.3k answers

8.7k comments

6.3k users

Free Hit Counters
...