C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Point Out Correct Statements (Q.No. 1)
Which of the following statement is correct about the program?

int main()
    FILE *fp;
    char ch;
    int i=1;
    fp = fopen("myfile.c", "r");
        if(ch == '\n')
    return 0;
The code counts number of characters in the file
The code counts number of words in the file
The code counts number of blank lines in the file
The code counts number of lines in the file
Answer: Option

This program counts the number of lines in the file myfile.c by counting the character '\n' in that file.

8 comments Page 1 of 1.

Sunanda said:   7 years ago
Whenever it will encounter \n i.e newline, it will increment the i value.That means it counts each and every line in the file.

Rakhtar said:   7 years ago
How it counts the number of line, even if we see in while loop the condition will create an infinite loop?

Sindhika K said:   1 decade ago
Really good explanation. Thank you. @mahi.

Mahi said:   1 decade ago
Firstly we are opening the file fp using fp = fopen("myfile.c", "r");
This also reads the file("r")
Now whatever the value equated to ch is counted
And we know that '\n' indicates every new lines arrival
By keeping track of \n in the statements
If(ch == '\n')
It results in number of new lines accessed

Ram said:   1 decade ago
Some explanations are well, but some more are not to be understood, will you explain shortly.

Hemendra said:   1 decade ago
Any one explain the answer in detail.

Bill Roberts said:   1 decade ago
It appears that i++ is over one line below the if statement, if it is, than it counts the characters in the file, else it counts the number of newline characters. Newline characters are not always indicative of a line, on windows \r\n is a line where on *nix it is \n.

Koushik said:   1 decade ago
Any one know - How to find space between words in that file?

Post your comments here:

Your comments will be displayed after verification.