| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Counting
Hi,
please I know that it is supposed to be easy but I don't get the answer. I want to count the number oflines of an input file (fin) the file consists of sentences( max characters is 20). (one sentence by line) I wrote: Code:
lines=0; int ch; while((ch=(fgets(line,19,fin)) !=NULL)/*check each line*/ if(ch=='\0') lines++; Can someone help me please? Thankx B. |
|
#2
|
|||
|
|||
|
Quote:
Instead of this you can use getline() function straight away and then testify its validness ,whether it is NULL or not. and also use the condition whether EOF is reached or not.Rest all as you have done. |
|
#3
|
|||
|
|||
|
Cirus is right, that's proabably the easier way to do it.
If you wonder why your code doesent work, here's an explanation : fgets() returns a c-style string (ie, a char*). That means, it returns a string, and you treat it as if it is a single char. If you want your code to work the way you've written it, you should use fgetc(). It would look like this: Code:
lines=0; int ch; while((ch=(fgetc(fin)) !=NULL)/*check each line*/ if(ch=='\0') lines++; |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Counting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|